0
>>>sampregex = re.compile(r'Bat(wo)?man')
>>>batRegex.search(' Batman Batwoman')
<re.Match object; span=(1, 7), match='Batman'>
>>> m1 = sampregex.search('Batwoman and Batwoman')
>>> m1
<re.Match object; span=(0, 8), match='Batwoman'>
>>> m1 = sampregex.findall('Batman and Batwoman')
>>> m1
['', 'wo']

The findall function is detecting only the expression inside the parenthesis, I am not sure how to correct it, I tried using as follows but now it returned two separate groups:

>>> sampregex = re.compile(r'(Bat(wo)?man)')
>>> m1 = sampregex.findall('Batman and Batwoman')
>>> m1
[('Batman', ''), ('Batwoman', 'wo')]
Kutsit
  • 107
  • 7

0 Answers0