regexps: testing and creating MatchObjects in one fell swoop
Dan Schmidt
dfan at harmonixmusic.com
Fri Sep 8 12:01:13 EDT 2000
I'm fond of the perl idiom
if (/(\d+)\s+(\d+)/) {
($num1, $num2) = ($1, $2);
}
(Yep, I know I could compress this even further if I wanted to.)
In Python, it seems I have to do the following:
match = re.search (r"(\d+)\s+(\d+)", line)
if match:
(num1, num2) = match.groups()
I'd love to be able to write something like (and I know I can't):
if (match = re.search (r"(\d+)\s+(\d+)", line)):
(num1, num2) = match.groups()
This isn't a huge pain in the simple case, but it quickly becomes
annoying when I want to do the equivalent of
if (/(\d+)\s+(\d+)/) {
($num1, $num2) = ($1, $2);
} elsif (/(\w+)\s+(\w+)/) {
($word1, $word2) = ($1, $2):
} # etc.
as the two-part test in Python doesn't lend itself easily to a long
if/elif/elif chain.
I've "solved" the problem locally by using the following helper
function:
# research (regexp, string) is the same as regexp.search (string),
# but saves off the match results into 'rematch', so we can test for
# a regexp in an if statement and use the results immediately.
rematch = None
def research (regexp, string):
global rematch
rematch = regexp.search (string)
return (rematch != None)
So that I can write:
if research (r"(\d+)\s+(\d+)", line):
(num1, num2) = rematch.groups()
elif research (r"(\w+)\s+(\w+)", line):
(word1, word2) = rematch.groups()
# etc.
I suppose I can even inject research into the re module, and inject a
similar method into regular expression objects. etc, to make it nicer.
Is there a cleaner, or more approved, way, to accomplish this task?
If not, does it make any sense to have a re.last_match object that
automatically contains the last match, allowing, for example:
if re.search (r"(\d+)\s+(\d+)", line):
(num1, num2) = re.last_match.groups()
Or is that too side-effecty and non-Pythonic?
--
Dan Schmidt | http://www.dfan.org
Honest Bob CD now available! | http://www.dfan.org/honestbob/cd.html
More information about the Python-list
mailing list