-3

I searched a lot for a regex pattern which is convenient for me. I couldn't find any.

Here is my conditions,

1. email should not contain any space or special characters(excep @ and .)
2. email should not start with dot(.) or should not end with .(before @ symbot)
3. two dots should not come near.
4. @ symbol should not repeat.

Here some valid and invalid email IDS,

firstname@gmail.com -- valid
firstname.@gmail.com -- invalid(dot and @ should not come closer)
firstname..lastname@gmail.com --invlid(two dots should not come closer)
firstname#lastname@gmail.com -- invalid(should not contain any special characters appart @ and .)
frist1991@gmail.com -- valid
first name@gmai.com -- invalie( should not allow any spaces)
.fristname@gmail.com --invalid(should not start with .)

I found lot of regex pattern for email. but didn't satisfy my conditions

Thanks in advance,

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Sandeep Sudhakaran
  • 1,072
  • 2
  • 9
  • 22
  • 1
    What about writting your own pattern ? – Cid Feb 18 '20 at 13:55
  • For something like this, you'd get much better results if you simply put your question title into Google instead of Stackoverflow. There are hundreds of regexes out there for email addresses. They're not hard to find. Many frameworks and libraries come with one built in. However, you should double-check what is actually valid, because some of your "invalid" criteria are actually valid in email addresses, and you will find people who are using them. – Spudley Feb 18 '20 at 14:03
  • Please, have a look at these sites: [TLD list](https://www.iana.org/domains/root/db); [valid/invalid addresses](https://en.wikipedia.org/wiki/Email_address#Examples); [regex for RFC822 email address](http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html) – Toto Feb 18 '20 at 16:07
  • https://stackoverflow.com/q/201323/372239 – Toto Feb 18 '20 at 16:08

1 Answers1

2

This would work:

^[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*@[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*$

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

MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
  • @moys You came swinging with a knife, sorry (err, not sorry) for having a pistol handy. There's nothing wrong with asking a question but accusing my answer of not working without additional context is hardly the right way to engage in meaningful conversation. Comments like yours often times results in downvotes for answers like mine because people assume the commenter did their due diligence. – MonkeyZeus Feb 18 '20 at 15:16
  • Whoever votes based on someone else's comments should be ignored. It still doesn't match for me when i use this code in python `items = [ "firstname@gmail.com", "firstname.@gmail.com", "firstname..lastname@gmail.com", "firstname#lastname@gmail.com", "frist1991@gmail.com", "first name@gmai.com", ".fristname@gmail.com" "fris.tn.ame@gmail.com"] for item in items: print(re.findall('^[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*@[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*$',item))` – moys Feb 18 '20 at 15:19
  • Thanks for your answer. This was I expected. thanks a lot – Sandeep Sudhakaran Feb 19 '20 at 05:06
  • you forgot the dash – boecko May 15 '23 at 15:46
  • @boecko Hmm, where? – MonkeyZeus May 15 '23 at 16:18