Skip to content

Instantly share code, notes, and snippets.

@jdowner
Created February 2, 2015 00:52
Show Gist options
  • Save jdowner/c78d925546e964b4b1df to your computer and use it in GitHub Desktop.
Save jdowner/c78d925546e964b4b1df to your computer and use it in GitHub Desktop.
Named regex groups
import re
class Regex(object):
"""
This is a convenience class for wrapping a regular expression, naming the
groups in the expression, and retrieving a named tuple as the result of a
match. For example,
>>> regex = Regex('filename: (\S*)', 'filename')
>>> regex.match('filename: my-file.txt')
RegexResult(filename='my-file.txt')
While, there is support in the re package for named groups, it can be useful
to separate the naming of the groups from the expressions themselves. In
particular, it makes it easier to read the regular expression. It is also
nice to be able to get the results of the match or search in a named tuple.
"""
def __init__(self, pattern, names):
self.result = collections.namedtuple('RegexResult', names)
self.pattern = re.compile(pattern)
assert self.pattern.groups == len(self.result._fields)
def match(self, txt):
"""Return the matching groups
If this Regex object matches the text string, a named tuple is return
with the matching groups.
"""
result = self.pattern.match(txt)
if result is not None:
return self.result(*result.groups())
def search(self, txt):
"""Return the matching groups
If this Regex object matches the text string, a named tuple is return
with the matching groups.
"""
result = self.pattern.search(txt)
if result is not None:
return self.result(*result.groups())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment