Lars
Lars

Reputation: 13

Regex Pattern for a File Name

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.*.

Upvotes: 1

Views: 3509

Answers (6)

yu_sha
yu_sha

Reputation: 4410

abc\.\d+

\d means any digit.

Upvotes: 0

sorpigal
sorpigal

Reputation: 26086

\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.

Upvotes: 0

Boldewyn
Boldewyn

Reputation: 82734

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).

Upvotes: 0

Colin Fine
Colin Fine

Reputation: 3364

Assuming Perl regexp:

^abc\.\d+$

Upvotes: 1

chills42
chills42

Reputation: 14513

abc\.\d+ should match it

\. matches the .

\d matches any digit

Upvotes: 0

LukeH
LukeH

Reputation: 269318

Something like this:

^abc\.\d+$

Upvotes: 8

Related Questions