Non-capturing groups
As we've mentioned before, capturing content is not the only use of groups. There are cases when we want to use groups, but we're not interested in extracting the information; alternation would be a good example. That's why we have a way to create groups without capturing. Throughout this book, we've been using groups to create subexpressions, as can be seen in the following example:
>>>re.search("Españ(a|ol)", "Español") <_sre.SRE_Match at 0x10e90b828> >>>re.search("Españ(a|ol)", "Español").groups() ('ol',)
You can see that we've captured a group even though we're not interested in the content of the group. So, let's try it without capturing, but first we have to know the syntax, which is almost the same as in normal groups, (?:pattern)
. As you can see, we've only added ?:
. Let's see the following example:
>>>re.search("Españ(?:a|ol)", "Español") <_sre.SRE_Match at 0x10e912648> >>>re.search("Españ(?:a|ol)", "Español").groups...