开发者

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


Something like this:

^abc\.\d+$


Assuming Perl regexp:

^abc\.\d+$


abc\.\d+ should match it

\. matches the .

\d matches any digit


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


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


abc\.\d+

\d means any digit.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜