A useful, but painful, one-liner to edit money amounts
John Posner
jjposner at optimum.net
Thu Aug 5 14:31:40 EDT 2010
On 8/5/2010 12:36 PM, MRAB wrote:
> You don't need to reverse the string:
>
> def thous_format(integer_string):
> """
> add comma thousands separator(s) to an integer-valued string
> """
> return re.sub(r"(?<=\d)(?=(?:\d\d\d)+$)", ",", integer_string)
Nice! My first encounter with a look-behind! It appears that a slight
simplification is possible:
return re.sub(r"(?<=\d)(?=(\d\d\d)+$)", ",", integer_string)
There no harm in putting \d\d\d into a group, though the group is never
used.
-John
More information about the Python-list
mailing list