>>>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']
findall 函数只检测括号内的表达式,我不知道如何更正它,我尝试使用如下但现在它返回了两个单独的组:
>>> sampregex = re.compile(r'(Bat(wo)?man)')
>>> m1 = sampregex.findall('Batman and Batwoman')
>>> m1
[('Batman', ''), ('Batwoman', 'wo')]