[Python-3000] More wishful thinking
Talin
talin at acm.org
Sun Apr 16 07:37:16 CEST 2006
<skip <at> pobox.com> writes:
> Giovanni> I'm sure you were talking more generically, but as for this
> Giovanni> specific case (which is indeed very common), I always wondered
> Giovanni> why re couldn't export wrappers around bound methods of the
> Giovanni> last match object, so to allow:
>
> Giovanni> if re.match(...):
> Giovanni> x,y = re.group(1), re.group(2)
> Giovanni> elif re.match(..):
> Giovanni> ...
>
> Because then the re module wouldn't be thread safe.
>
> Skip
True, although I suppose that you could add a "last match" to
regular expression objects:
re_pattern = re.compile( ... )
if re_pattern.match( string ):
text = re_pattern.lastmatch.group( 1 )
This violates the stateless nature of regular expression objects, which
is a minor wart. But other than that, the code seems cleaner to me -
in other words, we concern ourselves only test part of the match first,
and only after we've determined that there is a match, we start to look at
the specific attributes of the match.
-- Talin
More information about the Python-3000
mailing list