troubleshooting Question

Perl Regular Expersions in PHP

Avatar of credog
credog asked on
PerlPHPRegular Expressions
8 Comments1 Solution376 ViewsLast Modified:
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))
 return FALSE

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:
preg_match('|^[^0-9]{1,}$|', '$string');

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?
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 8 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros