Reading an online resource on PHP about Regexp(TuxRadar).
According to the author the following should not match “aaa1” to the pattern and therefore return false(0), but I get true(1).
<?php
$str = "aaa1";
print preg_match("/[a-z]+[0-9]?[a-z]{1}/", $str);
?>
Why?
Are you sure there isn’t supposed to be a trailing
$there? Without it, returning true makes a lot of sense – the first[a-z]block matches the first 2acharacters, the[0-9]matches nothing, and the last[a-z]matches the 3rda. The trailing1is ignored.Looking at the link to the book, it does seem there’s an error there:
This is only true if the regular expression is anchored to the end of the string with a
$.