Created
February 2, 2015 00:52
-
-
Save jdowner/c78d925546e964b4b1df to your computer and use it in GitHub Desktop.
Named regex groups
This file contains hidden or 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
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