Regular expressions, help?

Chris Angelico rosuav at gmail.com
Thu Apr 19 08:44:45 EDT 2012


On Thu, Apr 19, 2012 at 10:02 PM, Sania <fantasyblue82 at gmail.com> wrote:
> So now my regex is
>
>    dead=re.match(r".*death toll.{0,20}(\d[,\d\.]*)", text)
>
> But I only find 7 not 657. How is it that the group is only matching
> the last digit? The whole thing is parenthesis not just the last
> part. ?

Your problem is still that the early part is greedy. Either switch to
the non-greedy version or use something other than . such that it
can't match what you don't want it to match - \D will serve here.
(Both suggestions have been made already.) You don't need {0,20} that
way.

ChrisA



More information about the Python-list mailing list