regex question

MRAB google at mrabarnett.plus.com
Tue Aug 5 14:45:05 EDT 2008


On Aug 5, 11:39 am, Fred Mangusta <a... at bbb.it> wrote:
> Hi,
>
> I would like to delete all the instances of a '.' into a number.
>
> In other words I'd like to replace all the instances of a '.' character
> with something (say nothing at all) when the '.' is representing a
> decimal separator. E.g.
>
> 500.675  ---->       500675
>
> but also
>
> 1.000.456.344 ----> 1000456344
>
> I don't care about the fact the the resulting number is difficult to
> read: as long as it remains a series of digits it's ok: the important
> thing is to get rid of the period, because I want to keep it only where
> it marks the end of a sentence.
>
> I was trying to do like this
>
> s=re.sub("[(\d+)(\.)(\d+)]","... ",s)
>
> but I don't know much about regular expressions, and don't know how to
> get the two groups of numbers and join them in the sub. Moreover doing
> like this I only match things like "345.000" and not "1.000.000".
>
> What's the correct approach?
>
I would use look-behind (is it preceded by a digit?) and look-ahead
(is it followed by a digit?):

s = re.sub(r'(?<=\d)\.(?=\d)', '', s)



More information about the Python-list mailing list