I would like to find a solution to remove an extra dot in the email address.
e.g. apple@yahoo..com ===> apple@yahoo.com
How to remove the extra dot after the word 'yahoo'?
I would like to find a solution to remove an extra dot in the email address.
e.g. apple@yahoo..com ===> apple@yahoo.com
How to remove the extra dot after the word 'yahoo'?
To remove duplicated dots, I would suggest a regex replace:
email = 'apple@yahoo..com'
email = re.sub(r'[.]{2,}', '.', email)
print(email) # apple@yahoo.com
The regex pattern [.]{2,}
targets two or more continuous dots, and then we replace with just a single dot.
a simple solution for that is to use the method .replace, as below
myString = "john.doe@yahoo..com"
myString.replace("..com", ".com")
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 = 'apple@yahoo..com'
email = re.sub(r'\.+', '.', email)
print(email) # apple@yahoo.com
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 = 'dds..apple@yahoo..com'
email = re.sub(r'\.+(?=\w+$)', '.', email)
print(email) # dds..apple@yahoo.com
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 $