Regular Expression

Associate
Joined
14 Oct 2008
Posts
416
I'm trying to learn about regular expressions at the moment but just can't get my head around them!

I need to find a way of making sure a 4 digit number contains only the numbers 0, 5 and 9... so it must have all 3 and only all 3 numbers within the 4 digits (obviously any one of the 3 can be repeated to make the 4 digits total).

Hope that makes sense. I'm sure there was a much easier way of explaining it!

Any help appreciated.
 
Last edited:
Yeah, I did try that earlier. It brings up false positives as robmiller says unfortunately :(

So I just need a way of saying "0 AND 5 AND 9" instead of OR. Google doesn't seem to help on this occasion though!
 
What language are you writing this in? Maybe better to do this with conditional statements and some regex.
 
Last edited:
What language are you writing this in? Maybe better to do this with conditional statements and some regex.

PHP at the moment... but I could do it in Java or C# as well, I'm just doing it to learn about regular expressions really, nothing important.
 
It's a bit (very) dirty, but you could:

^(0059|0095|0509|0559|0590|0595|0599|0905|0950|0955|0959|0995|5009|5059|5090|5095|5099|5509|5590|5900|5905|5909|5950|5990|9005|9050|9055|9059|9095|9500|9505|9509|9550|9590|9905|9950){1}$

;) :o
 
Haha, I don't mind a bit of dirty programming once in a while... but that's a bit too far for my liking!

I've done it like this for now:

if(preg_match("/^[059]{4}$/",$j)){
if(stristr($j, '0') && stristr($j, '5') && stristr($j, '9')){
echo $j . "<br />";
}
}

Even that's a bit dirty, but I think it's working.
 
Back
Top Bottom