3 number and dot..

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Thu Nov 1 00:32:48 CET 2007


Hrvoje Niksic:
> I'm surprised that no one has proposed a regex solution, such as:

I presume many Python programmers aren't much used in using REs.


> >>> import re
> >>> re.sub(r'\d{1,3}(?=(?:\d{3})+$)', r'\g<0>.', str(1234567))
> '1.234.567'

It works with negative numbers too. It's a very nice solution of a
simple problem that shows what you can do with REs. But I think that
RE has to be expanded & splitted (and maybe even commented) with a
VERBOSE, to improve readability, maybe somethign like:

\d {1,3}
(?=                # lockahead assertion
  (?: \d {3} )+ $  # non-group
)

Bye,
bearophile




More information about the Python-list mailing list