匹配python正则表达式中的括号 [英] matching parentheses in python regular expression

查看:91
本文介绍了匹配python正则表达式中的括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类似的东西

商店

结束行如 1 个商店 ..我想匹配,它使用 python 正则表达式.

ending line like 1 store(s) .. I want to match , it using python regular expression.

我尝试了类似 re.match('store\(s\)$',text)但它不起作用.

I tried something like re.match('store\(s\)$',text) but it's not working .

添加代码,我试过

import re
s = '1 store(s)'
if re.match('store\(s\)$',s) :
    print('match')

请帮忙.

谢谢极乐

推荐答案

或多或少直接回复您的评论

In more or less direct reply to your comment

试试这个

import re
s = '1 stores(s)'
if re.match('store\(s\)$',s):
    print('match')

解决方案是使用 re.search 而不是 re.match 因为后者试图用正则表达式匹配整个字符串,而前者只是试图找到一个与表达式匹配的字符串内的子字符串.

The solution is to use re.search instead of re.match as the latter tries to match the whole string with the regexp while the former just tries to find a substring inside of the string that does match the expression.

这篇关于匹配python正则表达式中的括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆