Skip to content

Instantly share code, notes, and snippets.

@Hanaasagi
Last active March 19, 2017 03:47
Show Gist options
  • Save Hanaasagi/f34c0fa76628e959a4d452e52ba320ef to your computer and use it in GitHub Desktop.
Save Hanaasagi/f34c0fa76628e959a4d452e52ba320ef to your computer and use it in GitHub Desktop.
将正则表达式中的捕获组转换为非捕获组
# 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