RegExLib.com - The first Regular Expression Library on the Web!

Please support RegExLib Sponsors

Sponsors

Regular Expression Details

Title Test Find Pattern Title
Expression
^((\d{5}-\d{4})|(\d{5})|([AaBbCcEeGgHhJjKkLlMmNnPpRrSsTtVvXxYy]\d[A-Za-z]\s?\d[A-Za-z]\d))$
Description
Regular expression for US (ZIP and ZIP+4) and Canadian postal codes. It allows 5 digits for the first US postal code and requires that the +4, if it exists, is four digits long. Canadain postal codes can contain a space and take form of A1A 1A1. The letters can be upper or lower case, but the first letter must be one of the standard Canadian zones: A,B,C,E,G,H,J,K,L,M,N,P,R,S,T,V,X,Y.
Matches
00501 | 84118-3423 | n3a 3B7
Non-Matches
501-342 | 123324 | Q4B 5C5
Author Rating: The rating for this expression. Matthew Aznoe
Source
Your Rating
Bad Good

Enter New Comment

Title

Name

Comment

Spammers suck - we apologize. Please enter the text shown below to enable your comment (not case sensitive - try as many times as you need to if the first ones are too hard):

Existing User Comments

Title: Postal Code Rules
Name: Adam
Date: 2/5/2008 2:48:31 PM
Comment:
Just being picky about the regex should we not be using: ^([AaBbCcEeGgHhJjKkLlMmNnPpRrSsTtVvXxYy]\d[AaBbCcEeGgHhJjKkLlMmNnPpRrSsTtVvXxYy]\s?\d[AaBbCcEeGgHhJjKkLlMmNnPpRrSsTtVvXxYy]\d)$ for canadian only (as the characters not allowed in the first letter aren't allowed in any subsequent ones due to issues with OCR machines)


Title: Postal Code Rules
Name: Adam
Date: 2/5/2008 2:37:16 PM
Comment:
Just being picky about the regex should we not be using: ^([AaBbCcEeGgHhJjKkLlMmNnPpRrSsTtVvXxYy]\d[AaBbCcEeGgHhJjKkLlMmNnPpRrSsTtVvXxYy]\s?\d[AaBbCcEeGgHhJjKkLlMmNnPpRrSsTtVvXxYy]\d)$ for canadian only (as the characters not allowed in the first letter aren't allowed in any subsequent ones due to issues with OCR machines)


Title: Postal Code Rules
Name: Adam
Date: 2/5/2008 2:35:42 PM
Comment:
Just being picky about the regex should we not be using: ^([AaBbCcEeGgHhJjKkLlMmNnPpRrSsTtVvXxYy]\d[AaBbCcEeGgHhJjKkLlMmNnPpRrSsTtVvXxYy]\s?\d[AaBbCcEeGgHhJjKkLlMmNnPpRrSsTtVvXxYy]\d)$ for canadian only (as the characters not allowed in the first letter aren't allowed in any subsequent ones due to issues with OCR machines)


Title: Postal Code Rules
Name: Yves Labrie
Date: 2/16/2006 11:32:04 AM
Comment:
I called Canada Post about this and they gave me the format and validation. Here it is: Format without the space: ========================= ^[A-Za-z][0-9][A-Za-z][0-9][A-Za-z][0-9]$ Validation without the space: ============================= ^[AaBbCcEeGgHhJjKkLlMmNnPpRrSsTtVvXxYy][1-9][AaBbCcEeGgHhJjKkLlMmNnPpRrSsTtVvWwXxYyZz][0-9][AaBbCcEeGgHhJjKkLlMmNnPpRrSsTtVvWwXxYyZz][0-9]$ Accepted Characters =================== Character #1 A --> ABCEGHJKLMNPRSTVXY Character #2 0 --> 123456789 Character #3 A --> ABCEGHJKLMNPRSTVWXYZ Character #4 0 --> 0123456789 Character #5 A --> ABCEGHJKLMNPRSTVWXYZ Character #6 0 --> 0123456789 Refused Characters ================== Character #1 A --> DFIOQUWZ Character #2 0 --> 0 Character #3 A --> DFIOQU Character #4 0 --> None Character #5 A --> DFIOQU Character #6 0 --> None


Title: Feedback on US/Canada RegEx for Postal
Name: Darryl
Date: 5/12/2004 3:27:41 PM
Comment:
Thanks, this is great! FYI, if you only want regex for Canada use this: ^([AaBbCcEeGgHhJjKkLlMmNnPpRrSsTtVvXxYy]\d[A-Za-z]\s?\d[A-Za-z]\d)$


Title: re: Postal Code Rules
Name: Matthew Aznoe
Date: 10/21/2003 7:38:56 PM
Comment:
I did some research, and the US postal code is required to be five digits with leading 0's. Also, the space for the Canadian postal code is required. I simplified the regular expression to search for invalid letters for Canadian postal codes instead of the valid ones. I have not yet been able to find information about further restrictions on Canadian postal codes.


Title: Postal Code Rules
Name: Remi Sabourin
Date: 10/21/2003 7:02:36 PM
Comment:
This regexp has promise. Can the first part of US postal codes be 3 digits? I thought they needed to be padded with zeros to always make 5. ie. 00501 instead of 501 I also know there are certain letters that are disallowed in the 3rd and 5th positions of canadian postal codes (I, O, Q?)


Copyright © 2001-2024, RegexAdvice.com | ASP.NET Tutorials