Stepping through regex pattern matches in Python:
In python:
Assignment is not allowed in conditionals.
The state of a regex match is determined based on a returned match object
that also contains other match info.
Now say we want to match a particular pattern out of 10 or 15, we end up
with something cluttered like this:
m = pat1.match(buffer)
if m:
tok = tok1
val = m.group(0)
else:
m = pat2.match(buffer)
if m:
tok = tok2
val = m.group(0)
else:
m = pat3.match(buffer)
if m:
tok = tok3
val = m.group(0)
else:
m = pat4.match(buffer)
if m:
tok = tok4
val = m.group(0)
else:
# ... keep indenting
We would really like to have something as follows:
if match ... pat1:
tok =
val =
elif match ... pat2:
tok =
val =
elif match ... pat3:
tok =
val =
...
(like can be done in other languages possibly using features like:
assignment in conditionals, side effect to a standard match object, a
different form of match function with pass by reference args ...)
We can maybe use a loop to run through the patterns, but that wouldn't be
suitable if there are variations in the processing for each match.
So: is there any nice pythonic way to keep the match conditionals at the
same level?!
No comments:
Post a Comment