My introduction into regular expressions.
Posted: Mon Sep 19, 2005 1:22 am
Edit: Actually, this whole post is an edit. 
I'm getting into regexes, so I might have more than my fair share of questions.
Firstly, I developed a scenario in my head. I want to see if a user has entered 2 consecutive numbers for their age. If they enter 3 numbers, then it's wrong. If they enter 1 number then it's wrong.
I've developed this regular expression.
This returns as I expect it to for more than 2 numbers. However if I put I'm 9 years old, it still returns true. How do I limit it to TWO numbers only?
Edit #2:
My second attempt. Also failed.
I'm getting into regexes, so I might have more than my fair share of questions.
Firstly, I developed a scenario in my head. I want to see if a user has entered 2 consecutive numbers for their age. If they enter 3 numbers, then it's wrong. If they enter 1 number then it's wrong.
I've developed this regular expression.
Code: Select all
<?
$string = 'Scott is 20 years old.';
echo $string;
echo "<BR><BR>";
if(preg_match("/\d{2}\d/",$string))
{
echo "You have entered an incorrect age.";
} ELSE
{
echo "You have entered a correct age.";
}
?>Edit #2:
Code: Select all
preg_match("/(\d)|(\d{3})/",$string)