Why re.match()?

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Thu Jul 2 06:14:36 CEST 2009


On Thu, 02 Jul 2009 03:49:57 +0000, kj wrote:

> In <Xns9C3BCA27ABC36duncanbooth at 127.0.0.1> Duncan Booth
> <duncan.booth at invalid.invalid> writes:
>>So, for example:
> 
>>>>> re.compile("c").match("abcdef", 2)
>><_sre.SRE_Match object at 0x0000000002C09B90>
>>>>> re.compile("^c").search("abcdef", 2)
>>>>>
>>>>>
> I find this unconvincing; with re.search alone one could simply do:
> 
>>>> re.compile("^c").search("abcdef"[2:])
> <_sre.SRE_Match object at 0x75918>
> 
> No need for re.match(), at least as far as your example shows.

Your source string "abcdef" is tiny. Consider the case where the source 
string is 4GB of data. You want to duplicate the whole lot, minus two 
characters. Not so easy now.


> Maybe there are times when re.match() is more "convenient" in some way,
> but it is awfully Perlish to "multiply language elements" for the sake
> of this trifling convenience.

No, it's absolutely Pythonic. 


>>> import this
...
Although practicality beats purity.



-- 
Steven



More information about the Python-list mailing list