RE Help

J. Clifford Dyer jcd at sdf.lonestar.org
Fri Sep 21 21:29:50 CEST 2007


On Fri, Sep 21, 2007 at 12:05:51PM -0700, chris.monsanto at gmail.com wrote regarding Re: RE Help:
> 
> >
> > x = re.compile('START(.*)END', re.DOTALL)
> 
> You'll want to use a non-greedy match:
> 
> x = re.compile(r"START(.*?)END", re.DOTALL)
> 
> Otherwise the . will match END as well.

The . will only consume END if there is another END to match later on in the string.  And then it's a question of desired fuctionality.  If the given string is: "abcdSTARTefgENDxyzENDhijk" do you want to match "STARTefgEND" (in which case you need a non-greedy match r".*?" )? or do you want to match "STARTefgENDxyzEND" (in which case you need a greedy match: r".*" )?

Cheers,
Cliff



More information about the Python-list mailing list