0

I am new to learning RE. Following is the python code snippet:

>>>m = re.match("([abc])+", "abc")
>>> m.group()
'abc'    ..........(1)
>>> m.groups()
('c',)    .........(2)
>>> m = re.findall("([abc])+", "abc")
>>> m
['c']    ..........(3)

From what I understand, m.group() ignores the presence of any parentheses and hence works like re.match("[abc]+", "abc"). This way (1) makes sense to me.

Please explain (2) and (3). Is it that in (2), the same subgroup keeps being over-written because there can be only one sub-group due to the presence of a single parenthesis?

Tushar Vazirani
  • 1,011
  • 13
  • 14
  • See [*Python re.findall behaves weird*](http://stackoverflow.com/a/31915134/3832970) to understand how `re.findall` works when a regex contains capturing groups. To understand the `m.groups()`, see [*Repeating a Capturing Group vs. Capturing a Repeated Group*](http://www.regular-expressions.info/captureall.html) and this SO question: [*Capturing repeating subpatterns in Python regex*](http://stackoverflow.com/questions/9764930/capturing-repeating-subpatterns-in-python-regex). – Wiktor Stribiżew Dec 01 '16 at 17:08
  • Answered my own question here: https://stackoverflow.com/questions/9764930/capturing-repeating-subpatterns-in-python-regex/46571685#46571685 – Tushar Vazirani Oct 04 '17 at 18:26

1 Answers1

2

When you capture group continuously , the regex engine remembers only the last group.So it matches a , b and c but remembers only c.You should instead do

([abc]+)

See demo.

https://regex101.com/r/TaYp4J/1

For (3) , findall returns only groups when there are groups in regex .So it will return only c as that was the last of the continuous groups.

vks
  • 67,027
  • 10
  • 91
  • 124