Why isn't the regular expression's "non-capturing" group working?

PythonRegex

Python Problem Overview


In the snippet below, the non-capturing group "(?:aaa)" should be ignored in the matching result,

The result should be "_bbb" only.

However, I get "aaa_bbb" in the matching result; only when I specify group(2) does it show "_bbb".

>>> import re
>>> s = "aaa_bbb"
>>> print(re.match(r"(?:aaa)(_bbb)", s).group())

aaa_bbb

Python Solutions


Solution 1 - Python

I think you're misunderstanding the concept of a "non-capturing group". The text matched by a non-capturing group still becomes part of the overall regex match.

Both the regex (?:aaa)(_bbb) and the regex (aaa)(_bbb) return aaa_bbb as the overall match. The difference is that the first regex has one capturing group which returns _bbb as its match, while the second regex has two capturing groups that return aaa and _bbb as their respective matches. In your Python code, to get _bbb, you'd need to use group(1) with the first regex, and group(2) with the second regex.

The main benefit of non-capturing groups is that you can add them to a regex without upsetting the numbering of the capturing groups in the regex. They also offer (slightly) better performance as the regex engine doesn't have to keep track of the text matched by non-capturing groups.

If you really want to exclude aaa from the overall regex match then you need to use lookaround. In this case, positive lookbehind does the trick: (?<=aaa)_bbb. With this regex, group() returns _bbb in Python. No capturing groups needed.

My recommendation is that if you have the ability to use capturing groups to get part of the regex match, use that method instead of lookaround.

Solution 2 - Python

group() and group(0) will return the entire match. Subsequent groups are actual capture groups.

>>> print (re.match(r"(?:aaa)(_bbb)", string1).group(0))
aaa_bbb
>>> print (re.match(r"(?:aaa)(_bbb)", string1).group(1))
_bbb
>>> print (re.match(r"(?:aaa)(_bbb)", string1).group(2))
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
IndexError: no such group

If you want the same behavior than group():

" ".join(re.match(r"(?:aaa)(_bbb)", string1).groups())

Solution 3 - Python

TFM:

class re.MatchObject

group([group1, ...])

Returns one or more subgroups of the match. If there is a single argument, the result is a single string; if there are multiple arguments, the result is a tuple with one item per argument. Without arguments, group1 defaults to zero (the whole match is returned). If a groupN argument is zero, the corresponding return value is the entire matching string.

Solution 4 - Python

Try:

print(re.match(r"(?:aaa)(_bbb)", string1).group(1))

group() is same as group(0) and Group 0 is always present and it's the whole RE match.

Solution 5 - Python

You have to specify group(1) to get just the part captured by the parenthesis (_bbb in this case).

group() without parameters will return the whole string the complete regular expression matched, no matter if some parts of it were additionally captured by parenthesis or not.

Solution 6 - Python

Use the groups method on the match object instead of group. It returns a list of all capture buffers. The group method with no argument is returning the entire match of the regular expression.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJim HorngView Question on Stackoverflow
Solution 1 - PythonJan GoyvaertsView Answer on Stackoverflow
Solution 2 - PythonRichard SimõesView Answer on Stackoverflow
Solution 3 - PythonPavel MinaevView Answer on Stackoverflow
Solution 4 - PythoncodaddictView Answer on Stackoverflow
Solution 5 - PythonsthView Answer on Stackoverflow
Solution 6 - PythonMattView Answer on Stackoverflow