1

Im trying to make a regex statement to validate email addresses. Here is my current statement:

[a-z|A-Z|.]+@[a-z|A-Z]+\.[a-z]{3}\.[a-z]{2}

I have been trying to prevent it from accepting multiple full stops in a row. But it isnt having any of it. Ive tried:

\.(?!\.)

However it recognises the first . as a required value unrelated to the ?! negative lookahead. Does anyone know a basic/simple negative lookahead (or any other way of achieving this) and is able to explain what it does. This would be much appreciated :) Thanks

  • 3
    If I were you, I wouldn't validate emails with regex. See: https://stackoverflow.com/questions/46155/how-to-validate-email-address-in-javascript – Carey Oct 29 '17 at 11:07
  • Now ive run in to this issue, despite it not being the best idea, i want to see it to the end :D – Tweakforce_LG Oct 29 '17 at 11:13
  • 1
    The part before the @ could be changed to `[a-zA-Z]+(\.[a-zA-Z]+)*` which would prevent more than one consecutive dot, and also a dot before the @. Of course this assumes you only want alphabets in the email ID. – Vasan Oct 29 '17 at 11:16
  • plz give an example that your regex accepts, which should not be accepted as per you – Mustofa Rizwan Oct 29 '17 at 11:21
  • How to validate email addresses with regex is a FAQ, here and elsewhere. Instead of rolling your own attempt and posting *yet another* question about a topic that literally has been discussed to death, read a couple of the existing questions and learn from them. – Tomalak Oct 29 '17 at 11:27
  • 1
    Validating e-mail addresses can be seen as either a solved problem - in this case use one of the existing, tried and tested solutions, ideally from a validation library - or as a matter of learning how regex works, in which case this question can be reduced to *"How can I use regex to prevent two consecutive same characters?"* - in which case this question has been answered. First Google hit: https://stackoverflow.com/questions/4897353/regex-to-disallow-more-than-1-dash-consecutively. In either case, please do not make us debug yet another regex for email addresses. – Tomalak Oct 29 '17 at 11:30

1 Answers1

2

You may try the following regex:

^(?!.*\.\.)[a-zA-Z.]+@[a-zA-Z]+\.[a-z]{3}\.[a-z]{2}$

I have added (?!.*\.\.) at the beginning to make sure that the entire regex doesn't have contiguous .

Explanation:

  1. ^ you must include start mark so that you don't get any partial match inside the email that you are looking for.
  2. (?!.*\.\.) negative look ahead regex to ensure that no two fullstop follows each other where .* makes sure that it keeps looking for such occurance till the the end of string.

  3. if the above condition is met which means there is no such repeat then your provided regex takes effect. But you have put [a-z|A-Z|.] pipes in your character class which will include pipe as a valid input as well. In a character class you dont need to separate sequence by pipe thus [a-zA-Z.] would do.

  4. $ ensures that the entire previous match is all you have... so ^.....$ start and end markers ensures that the format is entirely matched (no submatch).

Demo

Mustofa Rizwan
  • 10,215
  • 2
  • 28
  • 43
  • 1
    Note that this would allow the pipe symbol in the email ID (before the @). Even if this is allowable, it is redundantly declared in the character range before @ – Vasan Oct 29 '17 at 11:29
  • you are right, I actually didn't touch the op's regex part , updated the answer for you @Vasan – Mustofa Rizwan Oct 29 '17 at 11:30
  • 1
    Thank you :). I found a solution myself, however this one is way better as mine allowed sub matches. – Tweakforce_LG Oct 29 '17 at 19:08