pattern = "SUFFIX thatspecificword"
s = "word word word (doesn't matter how many words before) wordSUFFIX thatspecificword"
if s.endswith(pattern):
print("match found")
else:
print("match not found")
import re
pattern = "\wSUFFIX thatspecificword$"
s = "word word word (doesn't matter how many words before) wordSUFFIX thatspecificword"
if re.search(pattern, s):
print("match found")
else:
print("match not found")
2条答案
按热度按时间zpgglvta1#
这应该行得通:
正则表达式
参见demo
解释
.*
-搜索零个或多个任意类型的字符(\b.*SUFFIX\b)
-搜索以后缀结尾的单词(specificword)$
-搜索句末的"特定单词"。注:
如果您无法匹配整个单词为后缀的匹配项,则需要将正则表达式更改为:
.+
确保后缀前至少有一个字符yhqotfr82#
由于问题标签与python相关,将尝试从python视图中回答。
如果你只想字符串以
SUFFIX thatspecificword
结尾,那么你可以简单地编写如下代码-但是如果你想要以
wordSUFFIX thatspecificword
结尾的字符串,其中word
可以是任何单词,也就是说SUFFIX
必须是单词后缀,那么你可以看看python内置的名为re
的包,它处理正则表达式-