-2

I try to do this:

① batRegex = re.compile(r'Bat(wo)*+man') 

and this:

mo3 = batRegex.search('My name is Batwowowowowowomanman.')
mo3.group()

And I expect this:

'Batwowowowowowoman'

But I got this when I type ①:

re.error: multiple repeat at position 8

I wonder what that means. Because * means any times of repeat and + means at least one time repeat, that's exactly what happened in the mo3. What's wrong.

Thanks:)

My python version is Python 3.10.5.

1 Answers1

0

In your regex Bat(wo)*+man, the * already means to repeat the group (wo) zero or more times. Therefore, the following + is out of place. You probably intended to use Bat(?:wo)+man. Here is an updated script:

batRegex = re.compile(r'Bat(?:wo)+man') 
mo3 = batRegex.search('My name is Batwowowowowowomanman.')
print(mo3.group())  # Batwowowowowowoman
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360