RE Module

Roman rgelfand2 at hotmail.com
Sat Aug 26 22:22:14 EDT 2006


I looked at a book called beginning python and it claims that <.*?> is
a non-greedy match.

tobiah wrote:
> > In python's RE module, they're like Perl:
> >
> > Greedy:  "<.*>"
> > Nongreedy:  "<.*?>"
> >
>
> Oh, I have never seen that.  In that case, why
> did Roman's first example not work well for
> HTML tags?
>
> '<.*?>'
>
> Also, how does the engine decide whether I am adjusting
> the greed of the previous operator, or just asking
> for another possible character?
>
> Suppose I want:
>
> 	"x*?" to match "xxxxxxxO"
>
> If the '?' means non greedy, then I should get 'x' back.
> If the '?' means optional character then I should get
> the full string back.
>
> Checking in python:
>
> ######################################
> import re
>
> s = 'xxxxxxx0'
>
> m = re.search("x*", s)
> print "First way", m.group(0)
>
> m = re.search("x*?", s)
> print "Second way", m.group(0)
> #####################################
> First way xxxxxxx
> Second way
>
> So now I'm really confused.  It didn't do a non-greedy
> 'x' match, nor did it allow the '?' to match the '0'.
>
> 
> -- 
> Posted via a free Usenet account from http://www.teranews.com




More information about the Python-list mailing list