我有一个类似于
store(s)
的字符串,以 "1 store(s)" 结尾。 我想使用 Python 正则表达式进行匹配。
我尝试了像 re.match('store\(s\)$', text)
这样的代码,但它没有起作用。
这是我尝试过的代码:
import re
s = '1 store(s)'
if re.match('store\(s\)$', s):
print('match')
我有一个类似于
store(s)
的字符串,以 "1 store(s)" 结尾。 我想使用 Python 正则表达式进行匹配。
我尝试了像 re.match('store\(s\)$', text)
这样的代码,但它没有起作用。
这是我尝试过的代码:
import re
s = '1 store(s)'
if re.match('store\(s\)$', s):
print('match')
解决方案是使用Try this
import re s = '1 stores(s)' if re.match('store\(s\)$',s): print('match')
re.search
而不是re.match
,因为后者试图将整个字符串与正则表达式匹配,而前者只尝试查找一个子字符串,在其中找到与表达式匹配的内容。s = '1 stores(s)'
。我建议你复制粘贴引用……或者打字时更加小心,否则你的回答会很难理解。 - martineau你考虑过 re.match('(.*)store\(s\)$',text)
吗?