Skip to content

Instantly share code, notes, and snippets.

@gromgull
Created October 20, 2012 06:01
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gromgull/3922244 to your computer and use it in GitHub Desktop.
Save gromgull/3922244 to your computer and use it in GitHub Desktop.
Python "unmatched group" regex replace workaround
import re
def re_sub(pattern, replacement, string):
def _r(m):
# Now this is ugly.
# Python has a "feature" where unmatched groups return None
# then re.sub chokes on this.
# see http://bugs.python.org/issue1519638
# this works around and hooks into the internal of the re module...
# the match object is replaced with a wrapper that
# returns "" instead of None for unmatched groups
class _m():
def __init__(self, m):
self.m=m
self.string=m.string
def group(self, n):
return m.group(n) or ""
return re._expand(pattern, _m(m), replacement)
return re.sub(pattern, _r, string)
print re_sub('(ab)|(a)', r'(1:\1 2:\2)', 'abc')
# prints '(1:ab 2:)c'
@jeff-hykin
Copy link

This works impressively well, thank you! You have saved me many hours of attempting my own work arounds.

It took me a bit to realize what this was and how to use it (I am not terribly experienced in Python).
So just an FYI to others like myself;

Simply insert this code at the top of your code,
(get rid of line 26)
and then use re_sub instead of re.sub

@a455bcd9
Copy link

Thanks! It seems the sre_constants.error: unmatched group has been fixed in Python 3.5.

@dmhowcroft
Copy link

Is this code free for use and redistribution under something like Apache?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment