Regular expression match objects - compact syntax?

  • Thread starter Johann C. Rocholl
  • Start date
J

Johann C. Rocholl

Hello python-list,

I have a question about the match objects that are returned from the
match() method of compiled regular expression objects from the 're'
module. To parse Postscript T1 fonts that were disassembled into
plaintext, I came up with the following code:

import re
rmoveto = re.compile('^\s*(-?\d+)\s+(-?\d+)\s+rmoveto$')
rlineto = re.compile('^\s*(-?\d+)\s+(-?\d+)\s+rlineto$')
# ... other expressions with up to six paren groups

f = open(filename, 'r')
for line in f.readlines():

m = rmoveto.match(line)
if m:
x = x + int(m.group(1))
y = y + int(m.group(2))
glyph.append(('move', (x, y)))
continue

m = rlineto.match(line)
if m:
x = x + int(m.group(1))
y = y + int(m.group(2))
glyph.append(('line', (x, y)))
continue

# ... and so forth for the other expressions

Now here is my question: is there a simple way to join the following
two python code lines:
m = rmoveto.match(line)
if m:
into one single line like in the following:
if rmoveto.match(line):
x = x + int(rmoveto.group(1))
y = y + int(rmoveto.group(2))
glyph.append(('move', (x, y)))
elif rlineto.match(line):
# ...

The above syntax does not work because the compiled regular expression
object rmoveto doesn't provide a method called group(), as it comes
from module 're'. The obsolete package 'regex' did provide this, if I
read the docs correctly.

As a workaround, I also tried to use a nested function like this:

def match(expr):
m = expr.match(line)
return m

if match(rmoveto):
x = x + int(m.group(1))
# ...

This approach failed because the match function has its own local m,
so it didn't update the outer m. I couldn't use 'global m' either
because the whole thing, including the outer m, happens to be inside a
function, too.

How do you people handle this?

Thanks for your time,
Johann C. Rocholl
 
D

Diez B. Roggisch

Johann said:
How do you people handle this?

Usually we don't bothe too much. But it has been suggested to do something
like this:

class Matcher:
def __init__(self, rex):
self.rex = rex

def match(self, s):
self.m = self.rex.match(s)
return not self.m is None

Then you can do

m = Matcher(rex)

if m.match(line):
print m.m


Of course you can enhance the class mather so that all methods it does not
define by itself are delegated to self.m, which would be more convient. But
I'm currently too lazy to write that down :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top