Need help with a regular expression

marek.rocki at wp.pl marek.rocki at wp.pl
Wed Dec 19 07:18:41 EST 2007


On 19 Gru, 13:08, Sharun <sharun... at gmail.com> wrote:
> I am trying to find the substring starting with 'aaa', and ending with
> ddd OR fff. If ddd is found shouldnt the search stop? Shouldn't
> re5.search(str5).group(0) return 'aaa bbb\r\n ccc ddd' ?

The documentation for the re module (http://docs.python.org/lib/re-
syntax.html), tells you that the "*", "+", and "?" qualifiers are all
greedy; they match as much text as possible. What you are looking for
are the qualifiers "*?", "+?", "??". Your regex pattern might look
like this: "aaa.*?(ddd|fff)".

Regards,
Marek



More information about the Python-list mailing list