开发者

Regular expressions: Matching strings starting with dot (.)?

I am a complete Reg-exp noob, so please bear with me. Tried to google this, but haven't found it yet.

What would be an appropri开发者_JAVA技巧ate way of writing a Regular expression matching files starting with a dot, such as .buildpath or .htaccess?

Thanks a lot!


In most regex languages, ^\. or ^[.] will match a leading dot.


The ^ matches the beginning of a string in most languages. This will match a leading .. You need to add your filename expression to it.

^\.

Likewise, $ will match the end of a string.


You may need to substitute the \ for the respective language escape character. However, under Powershell the Regex I use is: ^(\.)+\/

Test case:

"../NameOfFile.txt" -match '^(\\.)+\\\/'

works, while

"_./NameOfFile.txt" -match '^(\\.)+\\\/'

does not.

Naturally, you may ask, well what is happening here?

The (\\.) searches for the literal . followed by a +, which matches the previous character at least once or more times.

Finally, the \\\/ ensures that it conforms to a Window file path.


It depends a bit on the regular expression library you use, but you can do something like this:

^\.\w+

The ^ anchors the match to the beginning of the string, the \. matches a literal period (since an unescaped . in a regular expression typically matches any character), and \w+ matches 1 or more "word" characters (alphanumeric plus _).

See the perlre documentation for more info on Perl-style regular expressions and their syntax.


It depends on what characters are legal in a filename, which depends on the OS and filesystem.

For example, in Windows that would be:

^\.[^<>:"/\\\|\?\*\x00-\x1f]+$

The above expression means:

  • Match a string starting with the literal character .
  • Followed by at least one character which is not one of (whole class of invalid chars follows)

I used this as reference regarding which chars are disallowed in filenames.


To match the string starting with dot in java you will have to write a simple expression

^\\..*

^ means regular expression is to be matched from start of string
\. means it will start with string literal "."
.* means dot will be followed by 0 or more characters

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜