regexp: match only if previous matched?

Carl Banks pavlovevidence at gmail.com
Mon Jun 23 18:19:41 EDT 2008


On Jun 23, 6:02 pm, cirfu <circularf... at yahoo.se> wrote:
> I need to extract prices froma html-document.
>
> [0-9]*\$ matches 112$ 45$ etc but also just a $. why that shouldnt
> really matter and it is unlikely anyway to appear a $sign with no
> price attahced to it I still want to prevent it.
>
> How do I avoid matching "$"? It has to be "nbr$".

The answer to your question is to use a + instead of *.  + matches 1
or more elements, * matches zero or more.

The second point to mention is that, at least where I come from, the
currency symbol comes before the number:

$112 and $45

In which case your regexp should be somehting like this: \$[0-9]+


Carl Banks



More information about the Python-list mailing list