猿问

提取字符之前的单词

我试图提取之前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')


qq_遁去的一_1
浏览 123回答 2
2回答

慕慕森

您可以使用\w+(?=[^\S\r\n]+Y\b)请参阅正则表达式演示。细节:\w+- 一个或多个字母/数字/下划线 -&nbsp;(?=[^\S\r\n]+Y\b)- 紧跟一个或多个除 CR 和 LF 之外的空格,然后Y作为整个单词(\b是单词边界)。查看Python 演示:import refoo = "this is IMP Y text\nand this is also IMP1 Y text\nthis is not so IMP2 N text\nY is not important"print(re.findall(r'\w+(?=[^\S\r\n]+Y\b)', foo))# => ['IMP', 'IMP1']

开心每一天1111

尝试使用:(\w+)(?=.Y)你可以在这里测试所以,完整的代码是:import rea="""this is IMP Y textand this is also IMP1 Y textthis is not so IMP2 N textY is not important"""print (re.findall(r"(\w+)(?=.Y)",a))输出:['IMP', 'IMP1']
随时随地看视频慕课网APP

相关分类

Python
我要回答