shortest match regexp operator anyone?

Bruce Hartweg bhartweg at yahoo.com
Thu Jul 12 05:53:07 EDT 2001


"Fredrik Lundh" <fredrik at pythonware.com> wrote in message
news:Wv237.4859$z21.493903 at newsc.telia.net...
> Harald Kirsch wrote:
> > 2) TASK: Find the first '<A>' and match, if it is followed by a 'B'
> >    SOLUTION: ???
> >
> > An approximation for (2) is '^[^<>A]+<A>B', but it does not match
> > 'A<A>B', which it should.
> >
> > With non-greedy matching, another approximation is '^.*?<A>B', however
> > this matches 'xx<A>y<A>B', although it should not.
>
>     (?:(?!<A>).)*<A>B
>

almost, you must anchor it to start of string or
it will match  "A>y<A>B"


^(?:(?!<A>).)*<A>B

will fail to match the example as requested.

Bruce






More information about the Python-list mailing list