Perl Regular Expersions in PHP
Posted on 2011-09-08
I have a php script that uses preg_match to test for certain inputs. I had something like this,
elseif(preg_match("/^[^a-zA-Z,-_0-9\. ]+$/D", $string))
Meaning anything but those characters will return false, but it wasn't behaivng as I would expect.
So for this question I'll break it down to the most simple example:
I understand this to mean that anything that does NOT start or end with a number will be matched.
So if string is "gggg" it is matched. If string is "9999" it is not matched (do to the carrot in the bracket).
But if string is "g0g" it is not matched. The string begins and ends with a letter so my thought is that it would be matched. Why does adding a number between the two letters cause this to not match. To me it seems that the beggining and end of line anchors are not respected.
Even passing characters like ^^^^ gets matched, But as soon as a number is added somewhere it is not matched.
I assume it's working correctly, but I'd like an explanation as to why it behaves like this. I am assuming that the anchors (^$) in this case does not actually mean begins with and ends with?