tags:

views:

89

answers:

6

A user can put a file in the server if the file name matches the following criteria:

It has to start with abc, then a dot, and a number.

Valid file names:

    abc.2344
    abc.111

Invalid:

    abcd.11
    abc.ab12

What would be the regex? I can't just use abc.*.

+8  A: 

Something like this:

^abc\.\d+$
LukeH
A: 

abc\.\d+ should match it

\. matches the .

\d matches any digit

chills42
A: 
abc\.\d+

\d means any digit.

yu_sha
And `.` will match any character, unless you escape it with a backslash!
LukeH
Of course. It was eaten by the script (-:
yu_sha
+1  A: 

Assuming Perl regexp:

^abc\.\d+$

Colin Fine
A: 

Or a bit more verbose (= readable):

^abc\.[0-9]+$

where square brackets denote groups of characters.

By the way: The caret (^) means "start" and the dollar means "end" of the string in question (sometimes ^ and $ can mean start and end of a single line. It depends).

Boldewyn
A: 

\d+ and [0-9]+ still fall afoul of his requirement that "abcd.11" be invalid.

In Perl you could say:

/^abcd.\d{3,}$/

To indicate "abcd." followed by at least 3 digits. Not all regex languages support this syntax so inspect your documentation.

Sorpigal
As far as I can see, he didn't limit the number of digits.
Boldewyn
Aha, you appear to be correct. My mistake, I scanned over the d without registering the difference.^abc\.\d+$ is correct.
Sorpigal