Page 1 of 1
Why is the following regex failing?
Posted: Sun Nov 04, 2007 10:36 am
by impulse()
Code: Select all
$word = "stephen";
if(ereg("^[a-z][A-Z][0-9]$", $word))
echo "Good";
else
echo "Bad";
It's returning "Bad". But I would expect it to return "Good" as it meets the condition of the regex, as far as I can see.
Posted: Sun Nov 04, 2007 10:48 am
by Kieran Huggins
your character classes are only set to match a string of 3 characters
try:
^[a-zA-Z0-9]+$
but further to that, you should really consider using the PCRE regex functions. They're the defacto standard. Shoud that be the case, you'd want something with delimiters:
Code: Select all
preg_match('/^[a-zA-Z0-9]+$/',$word)
Posted: Sun Nov 04, 2007 11:01 am
by impulse()
Thanks that worked.
I'm currently reading through chapters on regex in my books at the minute. I always chose not to read about it because I found regex scary and thought it was more of a choice thing to learn. But from the past 30 minutes I've been reading about it seems pretty cool and not as scary as I once thought it looked.
Posted: Sun Nov 04, 2007 11:46 am
by Kieran Huggins
regex is a strange and fickle beast, but like most things is just a collection of small patterns.
Stay with it, I hear it gives you
super powers.
Posted: Sun Nov 04, 2007 11:50 am
by impulse()
The comic was so inspiring.
I feel like a trainee superhero already.
A bit off-topic but have you ever read BOFL?
Posted: Sun Nov 04, 2007 9:07 pm
by Kieran Huggins
BOFL?