Last active
March 19, 2017 03:47
-
-
Save Hanaasagi/f34c0fa76628e959a4d452e52ba320ef to your computer and use it in GitHub Desktop.
将正则表达式中的捕获组转换为非捕获组
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# borrow from bottle.py | |
def _re_flatten(p): | |
''' Turn all capturing groups in a regular expression pattern into | |
non-capturing groups. ''' | |
if '(' not in p: return p | |
return re.sub(r'(\\*)(\(\?P<[^>]+>|\((?!\?))', | |
lambda m: m.group(0) if len(m.group(1)) % 2 else m.group(1) + '(?:', p) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment