current position, and fails otherwise. Remember that Python’s string literals also use a backslash followed by numbers to allow including arbitrary characters in a string, so be sure to use a raw string when incorporating backreferences in a RE. For example, the following RE detects doubled words in a string. >>> p =re.compile( r'\b(\w+) \s+\1\b') >>> p.search('Paris in the the spring ').group() 'the the' Backreferences like this aren’t often useful for just searching through a string — there are few text formats which repeat data in this way — but you’ll soon find out that they’re very useful when performing string substitutions. Elaborate REs may use many groups, both to capture substrings of interest, and to group and structure the RE itself. In complex REs, it becomes difficult to keep track of the group numbers. There are two features which help with this problem. Both of them use a common syntax for regular expression extensions, so we’ll look at that first. Perl 5 is well known for its powerful additions to standard regular expressions. For these new features the Perl developers couldn’t choose new single-keystroke metacharacters or new special sequences beginning with$_{\}$ without making Perl’s regular expressions confusingly different from standard REs. If they chose$_{&}$as a new metacharacter, for example, old expressions would be assuming that$_{&}$was a regular character and wouldn’t have escaped it by writing $_{\&}$or$_{[&]}$ . The solution chosen by the Perl developers was to use$_{(?...)}$ as the extension syntax. ? immediately after a parenthesis was a syntax error because the$_{?}$ would have nothing to repeat, so this didn’t introduce any compatibility problems. The characters immediately after the$_{?}$ indicate what extension is being used, so$_{(?=foo)}$ is one thing (a positive lookahead assertion) and$_{(?:foo)}$ is something else (a non-capturing group containing the subexpression $_{foo}$). Python supports several of Perl’s extensions and adds an extension syntax to Perl’s extension syntax. If the first character after the question mark is a$_{P}$, you know that it’s an extension that’s specific to Python. Now that we’ve looked at the general extension syntax, we can return to the features that simplify working with groups in complex REs. Sometimes you’ll want to use a group to denote a part of a regular expression, but aren’t interested in retrieving the group’s contents. You can make this fact explicit by using a non-capturing group:$_{(?:...)}$ , where you can replace the$_{...}$ with any other regular expression. 4.3 Non-capturing and Named Groups >>> m =re.match("([abc])+ ","abc") >>> m.groups() ('c',) >>> m =re.match("(?:[abc])+ ","abc") >>> m.groups() () Except for the fact that you can’t retrieve the contents of what the group matched, a non-capturing group behaves exactly the same as a capturing group; you can put anything inside it, repeat it with a repetition metacharacter such as * , and nest it within other groups (capturing or non-capturing).$_{(?:...)}$ is particularly useful when modifying an existing pattern, since you can add new groups without changing how all the other groups are numbered. It should be mentioned that there’s no performance difference in searching between capturing and non-capturing groups; neither form is any faster than the other. A more significant feature is named groups: instead of referring to them by numbers, groups can be referenced by a name. The syntax for a named group is one of the Python-specific extensions:$_{(?P<name>...)}$ . name is, obviously, the name of the group. Named groups behave exactly like capturing groups, and additionally associate a name with a group. The match object methods that deal with capturing groups all accept either integers that refer to the group by 12