KoalaJ
KoalaJ

Reputation: 155

Python regular expression remove whitespace around some punctuations

These are the examples:

s1 = "hello 11 : 23 pm" --> "hello 11:23 pm"

s2 = "hello 10, 000 dollars" --> "hello 10,000 dollars"

s3 = "hello 10. 213 percent" --> "hello 10.213 percent"

s4 = "hello 11 : 23 pm, 10, 000 dollars, 10. 213 percent" --> "hello 11:23 pm, 10,000 dollars, 10.213 percent"

How to use the regular expression to convert the left strings to the right ones?

Upvotes: 0

Views: 70

Answers (2)

Nick
Nick

Reputation: 147146

You can replace any punctuation character that is preceded and followed by a digit (with some number of spaces in between) with itself (without the spaces) using re.sub:

import re

s1 = "hello 11 : 23 pm" 
s2 = "hello 10, 000 dollars"
s3 = "hello 10. 213 percent" 
s4 = "hello 11 : 23 pm, 10, 000 dollars, 10. 213 percent"

for s in [s1, s2, s3, s4]:
    new = re.sub(r'(?<=\d)\s*([:,.])\s*(?=\d)', r'\1', s)
    print(new)

Output:

hello 11:23 pm
hello 10,000 dollars
hello 10.213 percent
hello 11:23 pm, 10,000 dollars, 10.213 percent

Upvotes: 4

mamen
mamen

Reputation: 1675

You could use regular expressions to solve the problem:

import re

input = "hello 11 : 23 pm, 10, 000 dollars, 10. 213 percent"

result = re.sub(r"(\d)\s*([:,.]){1}\s*(\d)", r"\1\2\3", input)

print(result)

Upvotes: 2

Related Questions