0

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'?

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Wenny
  • 29
  • 1

3 Answers3

3

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.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • 2
    Perhaps there could be say 3 dots in a sequence? In that case, my answer also has this covered. – Tim Biegeleisen Feb 25 '22 at 08:37
  • 1
    instead of `r'[.]{2,}'` you also can use `r'[.]+'` for the same effect – Silver Feb 25 '22 at 08:52
  • @Silver not the same effect. If only one dot, we don't need to do the replacement. – Tim Biegeleisen Feb 25 '22 at 08:53
  • This is great because it's not limited to the domain and therefore resolves, for example, a..b@mail..com – DarkKnight Feb 25 '22 at 09:04
  • @TimBiegeleisen so both do give same result as "." -> "." and "....." -> "." , that same effect to me, just different detailed behavior. – Silver Feb 25 '22 at 09:11
  • @Silver ... my version avoids the replacement on a single dot. It is just a minor optimization. – Tim Biegeleisen Feb 25 '22 at 09:12
  • 1
    @TimBiegeleisen true, `+` is equivalent to `{1,}` but with shorter syntax. I think it worth to be mentioned to regex newbie. – Silver Feb 25 '22 at 09:21
  • Escaping with `[.]` is not exactly the same as escaping with `\.`, and the former is not really meant to escape characters. It's ok to use it (not a big performance issue), and it's probably optimized into `\.` by the `re` module anyways, but still in a canonical answer I would suggest escaping it properly. – jthulhu Feb 25 '22 at 09:32
0

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

myString = "john.doe@yahoo..com"
myString.replace("..com", ".com")
Thomas
  • 174,939
  • 50
  • 355
  • 478
0

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 $

DDS
  • 2,340
  • 16
  • 34