setu shwetank
setu shwetank

Reputation: 93

Need to understand following regular expression output

I am new to python regular expression and going through python regular expression documentation. I am not able to comprehend the below code output - How 'c' got included as one of the groups. Below is the expression:

m = re.match("([abc])+", "abc")
print(m.groups())
print(m.group(1))

Output is:

('c',)
c

Upvotes: 0

Views: 61

Answers (1)

A. Bandtock
A. Bandtock

Reputation: 1241

It has to do with the greedy method of parsing regular expressions, in summary:

A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data

see https://regex101.com/r/ffBSOq/1

Upvotes: 3

Related Questions