import re
def test(stest):
pattern = re.compile(r'/product\/(.*?)/i')
result = pattern.match(stest)
if result:
print result.group()
else:
print "Doesn't match"
test('product/WKSGGPC104/GGPC-The-Paladin')
当我按照上面的方式运行编码时,结果将是“不匹配”,而不是“产品/”。
有人可以帮我吗?我已经使用在线工具测试了正则表达式,它显示良好并且与我要测试的字符串匹配。
感谢您的帮助。
最佳答案
您的代码存在一些问题。
首先,开头没有 /
,其次,您在调用之后提供修饰符:
import re
def test(stest):
pattern = re.compile(r'product/([^/]+)'. re.IGNORECASE)
result = pattern.match(stest)
if result:
print(result.group())
else:
print("Doesn't match")
test('product/WKSGGPC104/GGPC-The-Paladin')
关于python - 正则表达式的pattern.match不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39865885/