Regex 正则表达式仅在单词为';一个单词的后缀前面有一个以上的字母
我有一个正则表达式:Regex 正则表达式仅在单词为';一个单词的后缀前面有一个以上的字母,regex,Regex,我有一个正则表达式:wine($|\s |,|)+这很好,但是我遇到了一个问题,即它也将与猪匹配。因此,我只想匹配wine前面至少有2个字符的单词后缀,因此为了便于说明,它应该匹配像trwine,thisiswine,thewine这样的单词,而不是像猪,这是猪 当然我可以这样做: import re word = 'wine' string = 'wine' pattern = re.compile(".{}($|\s|,|;)+".format(word)) #word as suffix
wine($|\s |,|)+
这很好,但是我遇到了一个问题,即它也将与猪匹配。因此,我只想匹配wine前面至少有2个字符的单词后缀,因此为了便于说明,它应该匹配像trwine,thisiswine,thewine
这样的单词,而不是像猪,这是猪
当然我可以这样做:
import re
word = 'wine'
string = 'wine'
pattern = re.compile(".{}($|\s|,|;)+".format(word)) #word as suffix
match = pattern.search(string)
if match:
if len(match.group(0)) > len(word) + 1:
print(match)
else:
print('no match')
但这太难看了,我相信用正则表达式很容易做到,但我不知道怎么做。你可以使用类似正则表达式的正则表达式
pattern = re.compile(r"[^\W\d_]{{2,}}{}\b".format(word))
它将看起来像[^\W\d\u]{2,}wine\b
,请参阅
详细信息
[^\W\d][2,}
-2个或更多字母
wine
-wine
子字符串
\b
-单词边界
:
也许r“[^\W\d][{{2,}}}{}\b”。格式(word)
?见鬼,那太快了,是的,看起来很好。这是什么意思@WiktorStribiżeww为什么要使用u?如果它也可以在没有下划线的情况下工作?@CodeNoob[^\W\d_ww]
匹配一个字母<代码>\w
匹配数字、字母和。在Python2.x中,要匹配任何Unicode字母,请使用带有re.U
标志的[^\W\d\]
。在Python3中,默认情况下它将匹配任何Unicode字母。
import re
word = 'wine'
s = 'words like trwine, thisiswine, thewine and thus not things like swine, this is a swine'
pattern = re.compile(r"[^\W\d_]{{2,}}{}\b".format(word))
match = pattern.search(s)
if match:
print(match.group())
else:
print('no match')