Home > OS >  Why there is no group containing "foobar" in the result? [duplicate]
Why there is no group containing "foobar" in the result? [duplicate]

Time:09-21

import re
pattern = '(foo(bar)?) '
s = "foobarfoo"
L = re.findall(pattern, s)
#[('foo', 'bar')]

The whole matched text is "foobarfoo". Why there is no group containing "foobar" here?

CodePudding user response:

Because of the .

First, the regex consumes "foobar", the first capture group is set to "foobar", and the second group is set to "bar".

Then, the allows trying again. This time, the remaining "foo" is matched, setting the first group to "foo" and not setting the second group (in most languages I'm familiar with, the second group would get set to null/undefined at this point, but apparently python is different).

If you had set s to "foobarf" instead of "foobarfoo" then you would have gotten [('foobar', 'bar')].

Or, if you had removed the and set pattern = '(foo(bar)?)', then findall would have given you [('foobar', 'bar'), ('foo', '')] A repeated match where the entire pattern is governed by a repetition quantifier is probably not what you meant :)

  • Related