Home > front end >  How to remove an extra dot (.) character in email address by Python?
How to remove an extra dot (.) character in email address by Python?

Time:02-25

I would like to find a solution to remove an extra dot in the email address.

e.g. [email protected] ===> [email protected]

How to remove the extra dot after the word 'yahoo'?

CodePudding user response:

To remove duplicated dots, I would suggest a regex replace:

email = '[email protected]'
email = re.sub(r'[.]{2,}', '.', email)
print(email)  # [email protected]

The regex pattern [.]{2,} targets two or more continuous dots, and then we replace with just a single dot.

CodePudding user response:

a simple solution for that is to use the method .replace, as below

myString = "[email protected]"
myString.replace("..com", ".com")

CodePudding user response:

Based on Tim's answer: (it's more general: it replaces multiple dots in an address) To me, it's much more readable: \. matches a dot, means "one or more times"

Anyway: to practice with regex I tstrongly suggest visiting regexr.com it has a nice test field and it explain the syntax giving also a reference

email = '[email protected]'
email = re.sub(r'\. ', '.', email)
print(email)  # [email protected]

If you want to only check the multiple dot pattern in the second part of the address (preserve multiuple dots in the sender) you could use

email = '[email protected]'
email = re.sub(r'\. (?=\w $)', '.', email)
print(email)  # [email protected]

here \. matches one or more dots, but only if followed (positive lookaround (?=<pattern>)) by a alphanumeric sequence \w then by the end of the string $

  • Related