You are here: Home > Dive Into Python > Regular Expressions > Case study: Parsing Phone Numbers | << >> | ||||
Dive Into PythonPython from novice to pro |
So far you've concentrated on matching whole patterns. Either the pattern matches, or it doesn't. But regular expressions are much more powerful than that. When a regular expression does match, you can pick out specific pieces of it. You can find out what matched where.
This example came from another real-world problem I encountered, again from a previous day job. The problem: parsing an American phone number. The client wanted to be able to enter the number free-form (in a single field), but then wanted to store the area code, trunk, number, and optionally an extension separately in the company's database. I scoured the Web and found many examples of regular expressions that purported to do this, but none of them were permissive enough.
Here are the phone numbers I needed to be able to accept:
Quite a variety! In each of these cases, I need to know that the area code was 800, the trunk was 555, and the rest of the phone number was 1212. For those with an extension, I need to know that the extension was 1234.
Let's work through developing a solution for phone number parsing. This example shows the first step.
>>> phonePattern = re.compile(r'^(\d{3})-(\d{3})-(\d{4})$') >>> phonePattern.search('800-555-1212').groups() ('800', '555', '1212') >>> phonePattern.search('800-555-1212-1234') >>>
Always read regular expressions from left to right. This one matches the beginning of the string, and then (\d{3}). What's \d{3}? Well, the {3} means “match exactly three numeric digits”; it's a variation on the {n,m} syntax you saw earlier. \d means “any numeric digit” (0 through 9). Putting it in parentheses means “match exactly three numeric digits, and then remember them as a group that I can ask for later”. Then match a literal hyphen. Then match another group of exactly three digits. Then another literal hyphen. Then another group of exactly four digits. Then match the end of the string. | |
To get access to the groups that the regular expression parser remembered along the way, use the groups() method on the object that the search function returns. It will return a tuple of however many groups were defined in the regular expression. In this case, you defined three groups, one with three digits, one with three digits, and one with four digits. | |
This regular expression is not the final answer, because it doesn't handle a phone number with an extension on the end. For that, you'll need to expand the regular expression. |
>>> phonePattern = re.compile(r'^(\d{3})-(\d{3})-(\d{4})-(\d+)$') >>> phonePattern.search('800-555-1212-1234').groups() ('800', '555', '1212', '1234') >>> phonePattern.search('800 555 1212 1234') >>> >>> phonePattern.search('800-555-1212') >>>
The next example shows the regular expression to handle separators between the different parts of the phone number.
>>> phonePattern = re.compile(r'^(\d{3})\D+(\d{3})\D+(\d{4})\D+(\d+)$') >>> phonePattern.search('800 555 1212 1234').groups() ('800', '555', '1212', '1234') >>> phonePattern.search('800-555-1212-1234').groups() ('800', '555', '1212', '1234') >>> phonePattern.search('80055512121234') >>> >>> phonePattern.search('800-555-1212') >>>
The next example shows the regular expression for handling phone numbers without separators.
>>> phonePattern = re.compile(r'^(\d{3})\D*(\d{3})\D*(\d{4})\D*(\d*)$') >>> phonePattern.search('80055512121234').groups() ('800', '555', '1212', '1234') >>> phonePattern.search('800.555.1212 x1234').groups() ('800', '555', '1212', '1234') >>> phonePattern.search('800-555-1212').groups() ('800', '555', '1212', '') >>> phonePattern.search('(800)5551212 x1234') >>>
The next example shows how to handle leading characters in phone numbers.
>>> phonePattern = re.compile(r'^\D*(\d{3})\D*(\d{3})\D*(\d{4})\D*(\d*)$') >>> phonePattern.search('(800)5551212 ext. 1234').groups() ('800', '555', '1212', '1234') >>> phonePattern.search('800-555-1212').groups() ('800', '555', '1212', '') >>> phonePattern.search('work 1-(800) 555.1212 #1234') >>>
Let's back up for a second. So far the regular expressions have all matched from the beginning of the string. But now you see that there may be an indeterminate amount of stuff at the beginning of the string that you want to ignore. Rather than trying to match it all just so you can skip over it, let's take a different approach: don't explicitly match the beginning of the string at all. This approach is shown in the next example.
>>> phonePattern = re.compile(r'(\d{3})\D*(\d{3})\D*(\d{4})\D*(\d*)$') >>> phonePattern.search('work 1-(800) 555.1212 #1234').groups() ('800', '555', '1212', '1234') >>> phonePattern.search('800-555-1212') ('800', '555', '1212', '') >>> phonePattern.search('80055512121234') ('800', '555', '1212', '1234')
See how quickly a regular expression can get out of control? Take a quick glance at any of the previous iterations. Can you tell the difference between one and the next?
While you still understand the final answer (and it is the final answer; if you've discovered a case it doesn't handle, I don't want to know about it), let's write it out as a verbose regular expression, before you forget why you made the choices you made.
>>> phonePattern = re.compile(r''' # don't match beginning of string, number can start anywhere (\d{3}) # area code is 3 digits (e.g. '800') \D* # optional separator is any number of non-digits (\d{3}) # trunk is 3 digits (e.g. '555') \D* # optional separator (\d{4}) # rest of number is 4 digits (e.g. '1212') \D* # optional separator (\d*) # extension is optional and can be any number of digits $ # end of string ''', re.VERBOSE) >>> phonePattern.search('work 1-(800) 555.1212 #1234').groups() ('800', '555', '1212', '1234') >>> phonePattern.search('800-555-1212') ('800', '555', '1212', '')
<< Verbose Regular Expressions |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | |
Summary >> |