如何在Regex中使用插入式插入符号?

时间:2015-01-30 16:32:33

标签: python regex

我想在

中找到所有单独的单词“is”
"is this is that this that this that this is"

除了开始

之外,我发现这是我需要的任何地方

(?<= )is(?= |$)

这会导致括号https://regex101.com/r/vD5iH9/22

出错

(?<=^| )is(?= |$)

如何看待线的开头?

1 个答案:

答案 0 :(得分:3)

Python中的回溯断言需要固定宽度。意思是,你不能使用匹配字符串开头(0个字符)或空格(1个字符)的(?<=^| )

要执行您想要的操作,请尝试使用re.findall

>>> import re
>>> data = "is this is that this that this that this is"
>>> re.findall(r'\bis\b', data)
['is', 'is', 'is']
>>>

请注意,\b与字边界匹配。