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

python, regex

Solution

`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())`

Problem

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 ```

Original source

Related problems