troubleshooting Question

Perl Regular Expersions in PHP

Avatar of credog
credog asked on
Regular ExpressionsPerlPHP
8 Comments2 Solutions376 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?
Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 2 Answers and 8 Comments.
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>


Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 2 Answers and 8 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004