Home > Mobile >  Regex: How to mark multiple points but ignore points embedded in letters?
Regex: How to mark multiple points but ignore points embedded in letters?

Time:11-03

I am facing a matching problem... I am trying to mark the multiple dots in an incorrect mail address so that only the relevant dots appear:

[email protected] 
[email protected]

My approach (\.(?!(\.\w))) works correctly for several dots in a row, but still matches the single dot in [email protected]

Do you have an idea? Any help appreciated.

CodePudding user response:

If you want to match those dots in an email address assuming there should be at least an @ char present, and using a lookbehind is supported:

Note that this does not validate an email format.

\.(?=\.[^@\s]*@)|(?<=@[^\s@]*\.)\.

Explanation

  • \. Match a dot
  • (?=\.[^@\s]*@) Positive lookahead, assert a dot directly to the right and an @ char to the right without crossing whitespace chars
  • | or
  • (?<=@[^\s@]*\.) Assert an @ char to the left and a dot directly before matching the dot
  • \. Match a dot

Regex demo

CodePudding user response:

\.{2,} would match two or more consecutive dots.

https://regex101.com/r/In9Qzm/1

  • Related