re module non-greedy matches broken

lothar lothar at ultimathule.nul
Mon Apr 4 15:42:46 EDT 2005


no - in the non-greedy regex
  <1st-pat><not-1st-pat>*?<follow-pat>

<1st-pat>, <not-1st-pat> and <follow-pat> are arbitrarily complex patterns.

with character classes and negative character classes you do not need
non-greediness anyway.


"John Ridley" <ojokimu at yahoo.co.uk> wrote in message
news:mailman.1308.1112637220.1799.python-list at python.org...
>
> --- lothar <lothar at ultimathule.nul> wrote:
> > how then, do i specify a non-greedy regex
> >   <1st-pat><not-1st-pat>*?<follow-pat>
> >
> > that is, such that non-greedy part <not-1st-pat>*?
> > excludes a match of <1st-pat>
> >
> > in other words, how do i write regexes for my examples?
>
> Not sure if I completely understand your explanation, but does this get
> any closer to what your looking for?
>
> >>> vwre = re.compile("V[^V]*?W")
> >>> newdoc = "V1WVVV2WWW"
> >>> re.findall(vwre, newdoc)
> ['V1W', 'V2W']
>
> That is: <pat1>, then <not-pat1> as few times as possible, then <pat2>
>
>
> John Ridley
>
> Send instant messages to your online friends http://uk.messenger.yahoo.com






More information about the Python-list mailing list