1

Here is the deal...

I am suppose to parse a Canadian Postal Code (CapsLetterNumberCapsLetterNumberCapsLetterNumber: exemple A1B2C3 or G2V3V4) IF there is one.

I have this code (PHP):

//Create new SESSION variable to store a warning
$_SESSION['msg'] = "";
//IF empty do nothing, IF NOT empty parse, IF NOT match regex put message in msg
if(!preg_match('^([A-Z][0-9][A-Z][0-9][A-Z][0-9])?$^', $_POST['txtPostalCode']) && $_POST['txtPostalCode'] != "")
{
    $_SESSION['msg'] .= "Warning invalide Postal Code";
}

then the code goes on to display $_SESSION['msg'].

The problem is that whatever I enter in $_POST['txtPostalCode'] it NEVER get parse by the REGEX.

2 Answers 2

4

You made the entire capturing group optional:

^([A-Z][0-9][A-Z][0-9][A-Z][0-9])?$^
                                 ^

It's also not a good idea to use regex metadata characters as your delimiter. Try this regex, which matches an uppercase letter and a number three times:

/^((?:[A-Z][0-9]){3})$/

You don't need to make the capturing group optional because you handle the logic for when the user doesn't submit a code with the && $_POST['txtPostalCode'] != "" part of the if statement.

Finally, since you're not even using the matches from this regex, you don't need the capturing group:

/^(?:[A-Z][0-9]){3}$/
1
  • @AndréDion - Not according to the OP's specifications :) But, adding in space is a simple change.
    – nickb
    Commented Jul 9, 2013 at 13:43
3

Your regex will match invalid postal codes.

A quick Google search for "canadian postal code regex" bought up

^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$

You may also want to put your $_POST['txtPostalCode'] != "" condition first since there's no point in executing a regex if the value is empty to begin with.

Edit: As pointed out by the comments, the quantifiers are redundant:

^[ABCEGHJKLMNPRSTVXY]\d[A-Z] *\d[A-Z]\d$
5
  • That, my friend, is an abomination of a regex. Someone, help me unsee it please. x_X P.S.: ^(?:[A-Z]\d){3}$ Commented Jul 9, 2013 at 13:33
  • @MaximKumpan - The functionality of yours and this one is different. Notice how in the first character class, D and other letters are missing. I suppose that is because they are not valid in Canadian postal codes.
    – nickb
    Commented Jul 9, 2013 at 13:34
  • Very well, but why the {1}? They are redundant, no? Also, you can use an exclude group to make it more readable. Commented Jul 9, 2013 at 13:35
  • Yes, the quantifiers are pointless. Simplified a bit: ^[ABCEGHJKLMNPRSTVXY]\d[A-Z]\s*\d[A-Z]\d$
    – nickb
    Commented Jul 9, 2013 at 13:35
  • @AndréDion true, the letters are'nt all valid in Can Postal Code, did'nt think of it at first! If I could vote two answer I would! but nickb answers point out the ? in my first expression. removing does resolve my problem but I will use your regex. Thanks to you two for your help !
    – Sebastien
    Commented Jul 9, 2013 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.