From 3b96c0f379388ed026f8cb53fc060263dd3cc4c1 Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Wed, 8 Jul 2009 16:40:55 -0400 Subject: [PATCH] added None.groups() example --- regular-expressions.html | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/regular-expressions.html b/regular-expressions.html index 72d2340..8c3b755 100644 --- a/regular-expressions.html +++ b/regular-expressions.html @@ -306,11 +306,15 @@ body{counter-reset:h1 4} >>> phonePattern.search('800-555-1212').groups() ('800', '555', '1212') >>> phonePattern.search('800-555-1212-1234') ->>> +>>> phonePattern.search('800-555-1212-1234').groups() >#x2463; +Traceback (most recent call last): + File "<stdin>", line 1, in <module> +AttributeError: 'NoneType' object has no attribute 'groups'
    -
  1. 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. +
  2. 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, \d means “any numeric digit” (0 through 9). The {3} means “match exactly three numeric digits”; it’s a variation on the {n,m} syntax you saw earlier. Putting it all 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.
  3. 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() method 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.
  4. 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. +
  5. And this is why you should never “chain” the search() and groups() methods in production code. If the search() method returns no matches, it returns None, not a regular expression match object. Calling None.groups() raises a perfectly obvious exception: None doesn’t have a groups() method. (Of course, it’s slightly less obvious when you get this exception from deep within your code. Yes, I speak from experience here.)
 >>> phonePattern = re.compile(r'^(\d{3})-(\d{3})-(\d{4})-(\d+)$')