number or strings that contain the desired group’s name. Named groups are still given numbers, so you can retrieve information about a group in two ways: >>> p =re.compile( r'(?P<word> \b\w+\b)') >>> m =p.search('(((( Lots of punctuation ))) ') >>> m.group('word') 'Lots' >>> m.group(1) 'Lots' >>> m =re.match(r'(?P<first> \w+) (?P<last> \w+) ','Jane Doe') >>> m.groupdict() {'first': 'Jane', 'last': 'Doe'} InternalDate=re.compile( r'INTERNALDATE "' r'(?P<day>[ 123][0-9])-(?P<mon>[A-Z][a-z][a-z])- ' r'(?P<year>[0-9][0-9][0-9][0-9]) ' r' (?P<hour>[0-9][0-9]):(?P<min>[0-9][0-9]):(?P<sec>[0-9][0-9]) ' r'(?P<zonen>[-+])(?P<zoneh>[0-9][0-9])(?P<zonem>[0-9][0-9]) ' r'"') Additionally, you can retrieve named groups as a dictionary with groupdict() : Named groups are handy because they let you use easily-remembered names, instead of having to remember numbers. Here’s an example RE from the imaplib module: It’s obviously much easier to retrieve$_{m.group('zonem')}$ , instead of having to remember to retrieve group 9. The syntax for backreferences in an expression such as$_{(...)\1}$ refers to the number of the group. There’s naturally a variant that uses the group name instead of the number. This is another Python extension:$_{(?P=name)}$ indicates that the contents of the group called name should again be matched at the current point. The regular expression for finding doubled words,$_{\b(\w+)\s+\1\b}$ can also be written as$_{\b(?P<word>\w+)\s+(?P=word)\b}$ : Another zero-width assertion is the lookahead assertion. Lookahead assertions are available in both positive and negative form, and look like this: >>> p =re.compile( r'\b(?P<word> \w+) \s+(?P=word) \b') >>> p.search('Paris in the the spring ').group() 'the the' 4.4 Lookahead Assertions To make this concrete, let’s look at a case where a lookahead is useful. Consider a simple pattern to match a filename and split it apart into a base name and an extension, separated by a$_{.}$ . For example, in$_{news.rc}$ ,$_{news}$is the base name, and$_{rc}$ is the filename’s extension. The pattern to match this is quite simple: .*[.].*$ Notice that the$_{.}$needs to be treated specially because it’s a metacharacter, so it’s inside a character class to only match that specific character. Also notice the trailing$_{$}$ ; this is added to ensure that all the rest of the string must be included in the extension. This regular expression matches$_{foo.bar}$and$_{autoexec.bat}$ and$_{sendmail.cf}$ and$_{printers.conf}$ . (?=...) Positive lookahead assertion. This succeeds if the contained regular expression, represented here by$_{...}$ , successfully matches at the current location, and fails otherwise. But, once the contained expression has been tried, the matching engine doesn’t advance at all; the rest of the pattern is tried right where the assertion started. (?!...) Negative lookahead assertion. This is the opposite of the positive assertion; it succeeds if the contained expression doesn’t match at the current position in the string. 13