Ashwin Geet D'Sa
Ashwin Geet D'Sa

Reputation: 7369

Removing the . (full-stop) and , (commas) that occurs in-between numbers in python

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

Answers (1)

Code Maniac
Code Maniac

Reputation: 37755

You can use this pattern

(\d)[,.](\d)

enter image description here

Replace by \1\2

Regex Demo

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 digit

Replace by empty string

Regex Demo

Upvotes: 3

Related Questions