7.1

Table Of Contents
Examples
The following examples may be useful.
Only numbers
The following regular expression accepts only numbers as input:
[0-9]+
The expression step by step:
l [0-9] Numbers 0 through 9
l + one or multiple
Dutch zip code
The Dutch zip code consists of four numbers, one space and two uppercase letters (eg 1234 AB).
[0-9]{4}\s[A-Z]{2}
The expression step by step:
l [0-9]{4} Four numbers
l \s a single space ("" or [] or \s)
l [A-Z]{2} Two uppercase characters
Canadian zip codes
Canadian postal codes alternate between letter and number such as L0S 1E0. Some choose not to put in the space. And not
every letter is used as the first letter which designates region. The regions are (from east to west, then north):
A,B,C,E,G,H,J,K,L,M,N,P,R,S,T,V,X,Y.
\s*[a-ceghj-npr-tvxyA-CEGHJ-NPR-TVXY]\d[a-zA-Z](\s)?\d[a-zA-Z]\d\s*
The expression step by step:
l \s* Zero or more whitespaces
l [a-ceghj-npr-tvxyA-CEGHJ-NPR-TVXY] The regions letters
l \d One digit
l [a-zA-Z] One letter
l (\s)? Zero or one whitespace
l \d One digit
l [a-zA-Z] One letter
l \d One digit
l \s* Zero or more whitespaces
Introduction to Regular Expressions
©2010 Objectif Lune Inc - 193 -