PDA

View Full Version : Regex


Colin F
08-04-2004, 10:46 AM
Hi

I'm trying to get these stupid regular expressions to work on profile fields.

Could someone check these, I seem to have something wrong in them.

Postcode (4 Numbers, first 1-9, second, third and fourth 0-9)

/^[1-9][0-9]{3}$/

Telephone number In the format: 062/775 12 34 or 01/775 56 78

So that means: First digit: 0
second digit: 1-9
third digit (optional) 1-9
than a backslash: /
fourth digit: 1-9
fifth and sixth digit: 0-9
then a space:
seventh and eighth digit: 0-9
then a space:
ninth and tenth digit: 0-9

I have this:

/^[0]?[0-9]\/[1-9][0-9]{2}\s[0-9]{2}\s[0-9]{2}$/

Mobile Phone Number: In the format: 076/123 45 67

first digit: 0
second digit: 7
third digit: 6, 8 or 9
then a backslash: /
fourth digit: 1-9
fifth and sixth digit: 0-9
then a space:
seventh and eighth digit: 0-9
then a space:
ninth and tenth digit: 0-9

I have:

/^07[6-9]\/[1-9][0-9]{2}\s[0-9]{2}\s[0-9]{2}$/

Thanks for your time!

CarCdr
08-04-2004, 11:26 AM
Postal code /^[1-9][0-9]{3}$/ looks right. Are you sure there is not leading or trailing whitespace? Since you are matching the entire field with ^$, you may want to deal with the whitespace by including \s* at the beginning and end.

Telephone number one does not match the english you provide. For example, you say "First digit: 0, second digit: 1-9, third digit (optional) 1-9", but your regular expression says starts with a search for optnal zero, 0-9, then a forward slash.

I think this matches your english (but I have including test for leading/trailing whitespace):
/^\s*0[1-9][1-9]?\/[1-9]\d{2}\s\d{2}\s\d{2}\s*$/

Note that I prefer \d to [0-9], so I used that.

CarCdr
08-04-2004, 11:31 AM
Ooops, forgot the mobile telephone number one. This should work:

/^\s*07[689]\/[1-9]\d{2}\s\d{2}\s\d{2}\s*/

I did not read your expression in detail, just the english, but one thing I did notice was that you said "third digit: 6,8 or 9", but your test was for "[6-9]", which would include the digit 7.

Colin F
08-04-2004, 11:49 AM
I assume that vBulletin trims the fields anyway, so I might as well put that \s there, you're right.

Unfortunatelly, when I sign up I still get an error message for the mobile number (I think it checks that one first and only echoes out that error...

Oh, and thanks anyway.

I left the 7 in there because I didn't feel like figuring out how to get rid of it :)