Efficiently test for positive re.match then use the result?
elmlish
elmlish at netscape.net
Wed Mar 3 13:24:58 EST 2004
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,
--
~elmlish~
More information about the Python-list
mailing list