re.match
Bob Cannard
bob_cannard at mentor.com
Wed Mar 21 12:37:35 EST 2001
"Michael P. Soulier" wrote:
>
> Hey people. Could someone explain the purpose of the match function in the
> re module? If it's only difference from search is to match at the beginning of
> the line, why does it exist? That's what the ^ in the regexp is for.
I'd like to add my own recent experience as a Python newbie getting to
grips with its implementation of regexps. Once upon a time there was
a regular expression which matched a - well, let's call it a Thing.
There were 3 questions that needed to be asked:
- Does this string contain any Things? (re.search, obviously)
- Find all the Things in this string. (re.findall)
- Is this string a Thing? (re.match - er, no, that doesn't work,
it only asks if this string begins with a Thing.)
Life would have been perfect if re.match, or some similar function,
required a match of the entire string against the regular expression.
But it doesn't, and there doesn't seem to be a function that does,
so the regular expression spawned another with a dollar sign on the
end. (Curious assymetry.) Alternatively the regexp could have been
left uncompiled so it could be manipulated, but that would have
caused a loss of speed and generality in my lower-level functions.
Another possibility would have been to combine compiled regular
expressions, but there doesn't seem to be a way to do this. Does
anyone know of a function or piece of magic that I've missed?
Anyway, my point is that there does seem to be a case, from my
experience, for having different functions that do different things
with the same regular expression. If anything I'd like to see more
of them.
Cheers,
Bob.
More information about the Python-list
mailing list