Efficiently test for positive re.match then use the result?

E

elmlish

Hello all,

I'm currently befuddled as to how to efficiently test for a positive re.
match then use the results of that match in a function.

Mostly what I've seen people do is to first test for the match, and then
try matching again to get the results. This would seem to be pretty
inefficient to me.

I've tried making the match, then sending it to a variable, then testing
if the variable is good and then finally using it, but this still seems
overkill.

I'm also trying to use this in list comprehensions, mostly because they
are kind of fun. What I've got right now looks something like this.

alist = ['boo','hoo','choo']
[re.match('choo',line) for line in calist if re.match('choo',line)]
[<_sre.SRE_Match object at 0x11e218>]

This is a small test, but what I will be looking for various matches in
is a large special purpose text file.

Does anyone have input on how something like this _should_ be done?
thanks,
 
?

=?iso-8859-1?q?Karl_Pfl=E4sterer?=

Mostly what I've seen people do is to first test for the match, and then
try matching again to get the results. This would seem to be pretty
inefficient to me.

Where did you see that? Often you see code like:

m = re.match('foo', 'foobar')
if m: do_something with m
I've tried making the match, then sending it to a variable, then testing
if the variable is good and then finally using it, but this still seems
overkill.

It isn't; you can't have directly an return value from assignment in
Python like e.g in C, so you can't write code like:

if m = re.match('foo', 'foobar'): do_somethiing_with_m

Global vars are also normally not set from re-matching (you could write
your own matching function which sets a global var; but that's seldom a
good idea ).
I'm also trying to use this in list comprehensions, mostly because they
are kind of fun. What I've got right now looks something like this.
alist = ['boo','hoo','choo']
[re.match('choo',line) for line in calist if re.match('choo',line)]
[<_sre.SRE_Match object at 0x11e218>]
This is a small test, but what I will be looking for various matches in
is a large special purpose text file.
Does anyone have input on how something like this _should_ be done?
thanks,

I don't know how it _should_ be done but I can tell you how it _could_ be
done.

Use a class like the following:

class Matcher (object):
__slots__ = ('reg', 'match')

def __init__ (self, reg):
self.reg = reg
self.match = None

def __call__ (self, val):
self.match = self.reg(val)
if self.match:
return True

Now you could use it like:
alist = ['boo','hoo','choo']
reg = Matcher(re.compile('choo').match)
[reg.match for c in alist if reg(c)]

That's no overkill.

But if you wanted it even lighter you could use a closure (but don't
tell anyone :) )

def matcher (reg):
res = [None]
def fun (s):
m = reg(s)
if m: res[0] = m
return m
return res, fun

You use it like that:
res, reg = matcher(re.compile('choo').match)
[res[0] for c in alist if reg(c)]


Or you simply write:
reg = re.compile('choo').match
filter(None, [reg(line) for line in alist])
[<_sre.SRE_Match object at 0xb3df38>]


Even for a big list filter(None, ...) is fast.




KP
 
D

Daniel Dittmar

elmlish said:
I'm currently befuddled as to how to efficiently test for a positive
re. match then use the results of that match in a function.

If you do this often, you might want to define a generator function:

def reMatches (iterator, patternString):
rex = re.compile (patternString)
for text in iterator:
match = rex.match (text)
if match:
yield match

This can be used as
for match in reMatches (file (fname), 'href="([^"]*)"'):
print match.group (0)

Variations include:
- reSearch to use the search instead of the match method
- allow to pass a string or a compiled regular expression as the second
parameter to reMatches
- find all the matches in a line and returning each with a separate yield

Daniel
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top