Industrial
Industrial

Reputation: 42798

Regular expressions: Matching strings 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 appropriate way of writing a Regular expression matching files starting with a dot, such as .buildpath or .htaccess?

Upvotes: 22

Views: 29049

Answers (7)

Max
Max

Reputation: 334

If you are using javascript, regex might not be needed in this case. You could just use the startsWith() method:

if ('.buildpath'.startsWith('.')) {
  console.log('String starts with a dot');
}

Upvotes: 0

Porky
Porky

Reputation: 988

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.

Upvotes: 4

Mani
Mani

Reputation: 508

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

Upvotes: 1

Jon
Jon

Reputation: 437854

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.

Upvotes: 2

Wyatt Anderson
Wyatt Anderson

Reputation: 9913

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.

Upvotes: 1

ephemient
ephemient

Reputation: 205014

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

Upvotes: 26

jasonbar
jasonbar

Reputation: 13461

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.

Upvotes: 9

Related Questions