Reputation: 13
I'm looking to replace specific characters in numbers I'm extracting but I cannot figure out how to do so. Here "," is the float separator and (' or .) are thousands separators. I can match this way :
>>> myString = "I buy 456'123,45 then 45.654 then 123. I'm 30."
>>> re.findall(r"(?:\d+)(?:['|.]\d+)+(?:[,]\d+)?", myString)
["456'123,45", '45.654']
I want to replace in my string all thousands separator to get this :
>>> newString
"I buy 456123,45 then 45654 then 123. I'm 30."
I'm pretty sure I need to use groups and subgroups in order to replace what I want but I don't know how to deal with groups when "()+" is present, the length of the number can also be very long (e.g : 123'456'789'123'456'789,123)
Thanks
Upvotes: 1
Views: 55
Reputation: 626853
You may use re.sub
with
(?<=\d)['.](?=\d)
and replace with an empty string. See the regex demo.
Details
(?<=\d)
- (positive lookbehind) a digit must appear immediately to the left of the current location['.]
- a single quote or a dot(?=\d)
- (positive lookahead) a digit must appear immediately to the right of the current location.Python:
re.sub(r"(?<=\d)['.](?=\d)", "", myString)
Upvotes: 1