Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python replace middle digits with commas thousand separator

I have a string like this:

123456789.123456789-123456789

Before and after the decimal/hyphen there can be any number of digits, what I need to do is remove everything before the decimal including the decimal and remove the hyphen and everything after the hyphen. Then with the middle group of digits (that I need to keep) I need to place a comma thousands separators. 

So here the output would be: 

123,456,789

I can use lookarounds to capture the digits in the middle but then it wont replace the other digits and i'm not sure how to place commas using lookarounds. 

(?<=\.)\d+(?=-)

Then I figured I could use a capturing group like so which will work, but not sure how to insert the comma's

\d+\.(\d+)-\d+

How could I insert comma's using one of the above regex?

like image 903
user3856888 Avatar asked Feb 22 '26 15:02

user3856888


1 Answers

Don't try to insert the thousands separators with a regex; just pick out that middle number and use a function to produce the replacement; re.sub() accepts a function as replacement pattern:

re.sub(r'\d+\.(\d+)-\d+', lambda m: format(int(m.group(1)), ','), inputtext)

The , format for integers when used in the format() function handles formatting a number to one with thousands separators:

>>> import re
>>> inputtext = '123456789.123456789-123456789'
>>> re.sub(r'\d+\.(\d+)-\d+', lambda m: format(int(m.group(1)), ','), inputtext)
'123,456,789'

This will of course still work in a larger body of text containing the number, dot, number, dash, number sequence.

The format() function is closely related to the str.format() method but doesn't require a full string template (so no {} placeholder or field names required).

like image 142
Martijn Pieters Avatar answered Feb 24 '26 07:02

Martijn Pieters