我试图提取之前Y被边界分隔的任何单词。因为我试图使用(?m)标志将每一行视为单独的记录,并尝试捕获\w+的前瞻\s+Y,但我只能打印第一个匹配,而不是第二个匹配(IMP1)。
print(foo)
this is IMP Y text
and this is also IMP1 Y text
this is not so IMP2 N text
Y is not important
目前无果的尝试:
>>> m = re.search('(?m).*?(\w+)(?=\s+Y)',foo)
>>> m.groups()
('IMP',)
>>>
>>> m = re.search('(?m)(?<=\s)(\w+)(?=\s+Y)',foo)
>>> m.groups()
('IMP',)
>>>
预期结果是:
('IMP','IMP1')
慕慕森
开心每一天1111
相关分类