Page 1 of 1

preg_match not working

Posted: Sat Aug 23, 2003 6:20 pm
by camarosource
if (preg_match ("/1[2][3-6][37|67][7-9][L|N]\d\d\d\d\d\d/", $VIN)) {
echo "1967 - 1969 VIN entered";
} else {
echo "NOT a VALID 13 digit VIN entered";
exit;
}

This should be saying the following:

1st char = 1
2nd char = 2
3rd char = 3 - 6
4th & 5th chars = 36 or 67
6th char = 7 - 9
7th char = L or N
8th - 13th chars = NUMBERS

The problem resides in the [36|37]

How do I specify I want it to allow "36" or "67"?

Posted: Sat Aug 23, 2003 6:27 pm
by BigE
Change the [37|67] to use rounded brackets such as (37|67) Hope that helps.

Posted: Sat Aug 23, 2003 6:30 pm
by camarosource
yep. That fixed it. thanks! :)

Posted: Sat Aug 23, 2003 9:42 pm
by m3rajk
are you sure that's right?

i believe that's just 4th 4 or 5.

to have both, change (37|67) to (37|67){2}
or to [37,67]{2} or to [37,67][37,67]

the reason is didn't work in [] the first time but worked in () is becasue | is or and can only work in a setting that you're captuning the or. the reasonb it will owk in the [] that i gave is becasue mine is tsaying the range is comprised of those numbers (note how i use a , instead of a |)

Posted: Sun Aug 24, 2003 1:15 am
by BigE
No, that doesn't need to be done, when you use {2} that tells it two matches (exactly) of that type. That should only be used with square brackets (pattern matching). In this case, the (37|67) says "two numbers that are 3 then 7 or 6 then 7" Make sense?

Posted: Sun Aug 24, 2003 5:17 pm
by m3rajk
yes.

Posted: Sun Aug 24, 2003 6:20 pm
by Stoker
Remember that \ is an escape in PHP string types, so you should use \\d to get a literal \d

I would have used this instead:
/12[3-6](36|67)[7-9](L|N)[0-9]{6}/