0

How to write regex to match if only first character is . ?

I'v been trying this:

hide_file={.*}

But unfortunately, it will find all files that has . in it. For example:

/home/user
  .bashrc
  .bash_history
  some_text.csv
  foo.json

In this example I would like this regex to affect only first two files.

P.S

That's the requirement:

Supported regex syntax is any number of *, ? and unnested {,} operators. Regex matching is only supported on the last component of a path, e.g. a/b/? is supported but a/?/c is not. Example: deny_file={*.mp3,*.mov,.private}

2 Answers 2

3

Simply use

^\s*?\..*$

See http://regex101.com/r/oW1xP3 for a live demo

If you are sure there are no whitespaces in front of your input remove the \s*?

2
  • 1
    Seems like it should be \s*? not +, since there may-or-may-not-be spaces, no?
    – Joe
    Commented Dec 13, 2013 at 18:14
  • @chill0r hi, I just tried your regex and found some restrictions at the place where I'm using it (vsftpd server). I updated an answer, could you please help with this requirements?
    – Vor
    Commented Dec 13, 2013 at 18:20
3

The trick is to anchor ^ the regex to the beginning of the string.

^\. will match any string that begins with a period. *Note: * you will need to escape this regex appropriately for your programming language.

hide_file={^\.}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.