Re: .*? still greedy (perl,python)?
On Wed, Aug 30, 2000 at 02:16:36PM +0300, joost witteveen wrote:
>
> Hi,
> I noticed that .*? in perl and phython, if used at the beginning
> of an unanchored (without ^) regular expression still behaves half-greedy:
>
> perl:
> "hello" =~ /(.*?)l/;
> print "\$1 = $1\n";
>
> python:
> import re
> r=re.search("(.*?)l","hello")
> print "$1=",r.group(1)
>
> In both cases, "he" is printed, while the real minimal match obviously
> would result in an emtpy $1 or r.group(1). This is the behavour
> if I put the (.*?) at the end (ex.: /h(.*?)/)
Why ? The regexp you are using impose the constraint of having an 'l'
following the parentesized subexpression. Only "he" satisfies this
constraint.
Cheers,
Marco
--
Marco Pantaleoni, Open Source Developer, Linuxcare Italia spa
+39.049.8043411 tel, +39.049.8043412 fax
panta@linuxcare.com, http://www.linuxcare.com/
Linuxcare. Support for the revolution.
Reply to: