Skip to content

Commit

Permalink
clean up regexen
Browse files Browse the repository at this point in the history
  • Loading branch information
wimglenn committed Nov 25, 2023
1 parent 323adc3 commit 1749ac4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
9 changes: 4 additions & 5 deletions parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,7 @@ class RepeatedNameError(ValueError):


# note: {} are handled separately
# note: I don't use r'' here because Sublime Text 2 syntax highlight has a fit
REGEX_SAFETY = re.compile(r"([?\\\\.[\]()*+\^$!\|])")
REGEX_SAFETY = re.compile(r"([?\\.[\]()*+^$!|])")

# allowed field types
ALLOWED_TYPES = set(list("nbox%fFegwWdDsSl") + ["t" + c for c in "ieahgcts"])
Expand Down Expand Up @@ -399,15 +398,15 @@ def extract_format(format, extra_types):
return locals()


PARSE_RE = re.compile(r"""({{|}}|{\w*(?:(?:\.\w+)|(?:\[[^\]]+\]))*(?::[^}]+)?})""")
PARSE_RE = re.compile(r"({{|}}|{\w*(?:\.\w+|\[[^]]+])*(?::[^}]+)?})")


class Parser(object):
"""Encapsulate a format string that may be used to parse other strings."""

def __init__(self, format, extra_types=None, case_sensitive=False):
# a mapping of a name as in {hello.world} to a regex-group compatible
# name, like hello__world Its used to prevent the transformation of
# name, like hello__world. It's used to prevent the transformation of
# name-to-group and group to name to fail subtly, such as in:
# hello_.world-> hello___world->hello._world
self._group_to_name_map = {}
Expand Down Expand Up @@ -553,7 +552,7 @@ def _expand_named_fields(self, named_fields):
k = basename

if subkeys:
for subkey in re.findall(r"\[[^\]]+\]", subkeys):
for subkey in re.findall(r"\[[^]]+]", subkeys):
d = d.setdefault(k, {})
k = subkey[1:-1]

Expand Down
2 changes: 1 addition & 1 deletion tests/test_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_slice_access():
assert r[-5:5] == (1, 2, 3, 4)
assert r[:4:2] == (1, 3)
assert r[::-2] == (4, 2)
assert r[5:10] == tuple()
assert r[5:10] == ()


def test_named_access():
Expand Down

0 comments on commit 1749ac4

Please sign in to comment.