Java Reference
In-Depth Information
For this regular expression, you need to specify more than just the valid characters; you also need to
specify the format of the data. For example, all of the following are valid:
+1 (123) 123 4567
+1123123 456
+44 (123) 123 4567
+44 (123) 123 4567 ext 123
+44 20 7893 4567
The variations that your regular expression needs to deal with (optionally separated by spaces) are
shown in the following table:
The international number
+ ” followed by one to three digits (optional)
The local area code
Two to five digits, sometimes in parentheses (compulsory)
The actual subscriber number
Three to 10 digits, sometimes with spaces (compulsory)
An extension number
Two to five digits, preceded by x , xtn , extn , pax , pbx , or
extension , and sometimes in parentheses
Obviously, this won't work in some countries, which is something you'd need to deal with based on
where your customers and partners would be. The following regular expression is rather complex
(its length meant it had to be split across two lines; make sure you type it in on one line):
^(\+\d{1,3} ?)?(\(\d{1,5}\)|\d{1,5}) ?\d{3} ?\d{0,7}
( (x|xtn|ext|extn|pax|pbx|extension)?\.? ?\d{2-5})?$
You will need to set the case‐insensitive flag with this, as well as the explicit capture option.
Although this seems complex, if broken down, it's quite straightforward.
Let's start with the pattern that matches an international dialing code:
(\+\d{1,3} ?)?
So far, you're matching a plus sign (\+) followed by one to three digits (\d{1,3}) and an optional
space ( ? ). Remember that because the + character is a special character, you add a \ character
in front of it to specify that you mean an actual + character. The characters are wrapped inside
parentheses to specify a group of characters. You allow an optional space and match this entire
group of characters zero or one time, as indicated by the ? character after the closing parenthesis of
the group.
Next is the pattern to match an area code:
(\(\d{1,5}\)|\d{1,5})
This pattern is contained in parentheses, which designate it as a group of characters, and matches
either one to five digits in parentheses ( (\d{1,5}) ) or just one to five digits ( \d{1,5} ). Again, because
Search WWH ::




Custom Search