Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I get rid of repeating special characters with regular expressions?

I want to get rid of all the repetitive dots except the ones that were one dot.

Sources:

(1) "a... b."
(2) "a....... b... c."

Results I want:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

(1) "a b."
(2) "a b c."

Code:

import re

a = "a... b."
b = "a....... b... c."

result = re.sub("[^a-zA-Z0-9 \\.{1}]", "", a)
print(result)

result = re.sub("[^a-zA-Z0-9 \\.{1}]", "", b)
print(result)

result = re.sub("[^a-zA-Z0-9 ][\\.{2,}]", "", a)
print(result)

result = re.sub("[^a-zA-Z0-9 ][\\.{2,}]", "", b)
print(result)

Doesn’t work.

How can I do to get my results?

>Solution :

Below code can do the needed task

import re
result = re.sub("\\.{2,}","","a....b....c.d....e.")
print(result)

Result will be-
abc.de.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading