Help me with my regexp
I am trying to match filenames that look like this:
45.pdf
Or
45_2.pdf
So there is a positive integer, an optional underscore followed by another开发者_如何学编程 positive integer, a full stop and a string reperesenting an extension.
The problem is, my regexp is also matching 45_.pdf which I don't want to.
Here it is:
$aRegexp = '/[0-9]+_?[0-9]*\\.[a-z]+/';
//$aString = '55.pdf';
//$aString = '55_5.pdf';
$aString = '55_.pdf';
var_dump(preg_match($aRegexp, $aString)); // should return int(0)
Group the underscore and the second integer together, use +
instead of *
for the second integer to force a match, and optional-match the entire group with ?
, like so:
$aRegexp = '/[0-9]+(_[0-9]+)?\\.[a-z]+/';
精彩评论