Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
说我想匹配:
r'(?P<as>a+)'
如何仅使用正则表达式语法忽略大括号之间的所有匹配项:
"{aabaa} a" #I just want the last 'a' matched
正则表达式对于不匹配并不是真正的最佳选择。但是您可以使用以下技巧:
a+(?![^{}]*})
但如果有不平衡,这将不起作用{}。
{}
一种选择可能是在运行正则表达式之前删除大括号内的内容。
>>> pat = r'(?P<as>a+)' >>> string = "{aabaa} a" >>> m = re.search(pat, re.sub(r'\{.*?\}', '', string)) >>> m.group('as') 'a'