1

I am trying to match a regex for an email address with the following conditions.

  • String must not contain more than 40 characters.
  • String matches the format emailid@domain where both emailid and domain contains only lowercase English letters, digits, and period(.)
  • domain should contain at least one period(.)
  • both email id and domain must not contain any consecutive period (.)

So Far, I am able to fulfill only the second condition with this regex:

/^[a-z0-9.]+@[a-z0-9.-]+\.[a-zA-Z]{2,6}$/

Any idea, how can I complete the other conditions?

isherwood
  • 52,576
  • 15
  • 105
  • 143
Devilism
  • 147
  • 2
  • 4
  • 19
  • 1
    You should test email length before doing regex matching, imho. – Amessihel Sep 21 '20 at 15:45
  • 1
    You could try `^(?!.{41}$)[a-z0-9]+(?:\.[a-z0-9]+)*@[a-z0-9-]+(?:\.[a-z0-9-]+)*\.[a-zA-Z]{2,6}$` – The fourth bird Sep 21 '20 at 15:48
  • So `emailid` and `domain` can begin or end with a period? – Amessihel Sep 21 '20 at 15:50
  • 1
    Nice one, @Thefourthbird – Amessihel Sep 21 '20 at 15:53
  • @Devilism You are welcome. If any of the given answers solves your problem you might consider to [accept an answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) by clicking ✓ on the left of it and [upvote the answers](https://meta.stackexchange.com/questions/173399/how-to-upvote-on-stack-overflow) that you have considered to be helpful. – The fourth bird Sep 28 '20 at 12:10
  • @Thefourthbird I have already upvoted your comment which solved my issue. – Devilism Oct 13 '20 at 08:52
  • @Devilism Thank you, nice that it worked out. Feel free to also accepting the answer pressing the grey ✓ on the left of the answer. – The fourth bird Oct 13 '20 at 08:54

2 Answers2

2

You can use a single negative lookahead to make sure that the string does not contain 41 characters.

If you repeat the character class without a period 1 or more times, followed by optionally repeating a group that starts with a period, you prevent matching consecutive periods.

This part \.[a-zA-Z]{2,6}$ already makes sure that there is at least a single period.

^(?!.{41})[a-z0-9]+(?:\.[a-z0-9]+)*@[a-z0-9-]+(?:\.[a-z0-9-]+)*\.[a-zA-Z]{2,6}$

Regex demo

Note that because the - is still in this character class [a-z0-9-]+, consecutive hyphens are still possible. If you don't want to allow this, you can use

^(?!.{41})[a-z0-9]+(?:\.[a-z0-9]+)*@[a-z0-9]+(?:[.-][a-z0-9-]+)*\.[a-zA-Z]{2,6}$
The fourth bird
  • 127,136
  • 16
  • 45
  • 63
0

Use positive lookahead:

/(?=^[a-z0-9.]+@[a-z0-9.-]+\.[a-zA-Z]{2,6}$)(?=^.{1,40}$)/

reference: https://stackoverflow.com/a/469951/1031191 by Jason Cohen:

"(?=match this expression)(?=match this too)(?=oh, and this)"

tested on: https://regex101.com/

Barney Szabolcs
  • 11,179
  • 11
  • 61
  • 88