Reputation: 7369
How to remove the . (full-stop) and , (commas) that occurs in-between numbers in python?
Example Input: "The shirt costs 1,50,0, coat costs 1.5.00, and the cap costs 2.50."
Example output: "The shirt costs 1500, coat costs 1500, and the cap costs 250."
The solution Remove period[.] and comma[,] from string if these does not occur between numbers does the inverse of the above mentioned requirement.
While, re.sub('[.,]','',input)
removes all the commas and full-stops.
Upvotes: 0
Views: 2446
Reputation: 37755
You can use this pattern
(\d)[,.](\d)
Replace by \1\2
If there are numbers with multiple . or ,
you can use lookaround
(?<=\d)[,.](?=\d)
(?<=\d)
- Match must be preceded by digit characters[,.]
- Match , or .
(?=\d)
- Match must be followed by digitReplace by empty string
Upvotes: 3