Over the last few years I have converted from Perl and Scheme to
Python. There one task that I do often that is really slick in Perl
but escapes me in Python. I read in a text line from a file and check
it against several regular expressions and do something once I find a match.
For example, in perl ...
if ($line =~ /struct {/) { do something } elsif ($line =~ /typedef struct {/) { do something else } elsif ($line =~ /something else/) { } ...
I am having difficulty doing this cleanly in python. Can anyone help?
I had a similar situation along with the requirement that the text to be scanned was being read in chunks. After looking at the Python re module and various other regex packages, I eventually wrote my own multiple pattern scanning matcher.
However, since then I've discovered that the sre Python module has a Scanner class that does something similar.
Anyway, you can see my code at: http://users.cs.cf.ac.uk/J.P.Giddy/python/Trespass/2.0.0/
Using it, your code could look like:
# do this once
import Trespass
pattern = Trespass.Pattern()
pattern.addRegExp(r'struct {', 1)
pattern.addRegExp(r'typedef struct {', 2)
pattern.addRegExp(r'something else', 3) # do this for each line
match = pattern.match(line)
if match:
value = match.value()
if value == 1:
# struct
do something
elif value == 2:
# typedef
do something
elif value == 3:
# something else
do something
else:
error
--
http://mail.python.org/mailman/listinfo/python-list
