Why re.match()?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Jul 3 10:19:23 CEST 2009


On Thu, 02 Jul 2009 11:19:40 +0000, kj wrote:

> I'm sure that it is possible to find cases in which the *current*
> implementation of re.search() would be inefficient, but that's because
> this implementation is perverse, which, I guess, is ultimately the point
> of my original post.  Why privilege the special case of a
> start-of-string anchor?  

Because wanting to see if a string matches from the beginning is a very 
important and common special case.


> What if you wanted to apply an end-anchored
> pattern to some prefix of your 4GB string?  Why not have a special re
> method for that?  And another for every possible special case?

Because they're not common special cases. They're rare and not special 
enough to justify special code.


> If the concern is efficiency for such cases, then simply implement
> optional offset and length parameters for re.search(), to specify any
> arbitrary substring to apply the search to.  To have a special-case
> re.match() method in addition to a general re.search() method is
> antithetical to language minimalism, and plain-old bizarre.  Maybe
> there's a really good reason for it, but it has not been mentioned yet.

There is, and it has. You're welcome to keep your own opinion, but I 
don't think you'll find many experienced Python coders will agree with it.


-- 
Steven



More information about the Python-list mailing list