-4

I already have an email address regular expression FROM RFC 2822 FORMAT

[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?

but want to modify it to include the following some new conditions:

  1. at least one full stop
  2. at least one @ character
  3. no consecutive full stops
  4. must not start/end with special characters i.e. should only start/end with [0-9a-zA-Z]
  5. should still follow RFC specification for regular expression rules.

Currently the above one allows the email to start with special characters. Also it is allowing two consecutive full stops (except for domain name which is fine, so test@test..com fails and its correct).

Thanks.

Varun Sharma
  • 2,591
  • 8
  • 45
  • 63
  • 2
    nice to tell us what you want to do, if you have an problems, maybe come back and show us what's not working for you? – Keith Nicholas Sep 07 '16 at 23:38
  • 1
    I'd recommend starting from proper regex (from http://stackoverflow.com/questions/20771794/mailrfc822address-regex), and as @KeithNicholas pointed out you need to have more than just list of requirements in the post. – Alexei Levenkov Sep 07 '16 at 23:40
  • 1
    I don't think you can do all that with a single regex – Burt_Harris Sep 07 '16 at 23:41
  • @Keith - 1. it is allowing two consecutive full stops and 2. it is allowing emails to start and end with special characters. It should only allow [0-9a-zA-Z] at the beginning and end of the email. So $test@test.com is invalid but test%test@test.c%om is valid. – Varun Sharma Sep 07 '16 at 23:41
  • sure it's allowing it.... what have you done to try and stop it? – Keith Nicholas Sep 07 '16 at 23:42
  • @Keith - I wrote a separate one and so there are two separate ones. I would like to have 1 regular expression. But if its not possible as Burt said then its fine. – Varun Sharma Sep 07 '16 at 23:44
  • This is not a question. It's a statement that you *want to modify it*. Are you asking permission to do so? Go ahead; it's OK with me if you modify it to meet those requirements. – Ken White Sep 08 '16 at 00:00
  • @Ken - No I am not asking for permission. I tried modifying it but it is breaking the existing functionality which is why asked here. Let me know if anything else is not clear to you. – Varun Sharma Sep 08 '16 at 00:04
  • 2
    As I said, you **have not asked a question**. You've **made a statement**. This is a *question and answer* site, which means there should be a clear, specific question asked. You've not done so, and you've not shown any indication of what you've tried (you haven't even said you did in the question) or explained how that effort didn't work. All of that explains my last comment. Let me know if anything else is not clear to you. – Ken White Sep 08 '16 at 00:09
  • 1
    You should take a look at [this](http://stackoverflow.com/documentation/regex/3605/useful-regex-showcase/12416/match-an-email-address#t=201609080730531388617) – Thomas Ayoub Sep 08 '16 at 07:31
  • @Ken White - Okay. It's clear to me. – Varun Sharma Nov 23 '16 at 23:46
  • @Thomas - Thanks, there was a detailed explanation there. Thanks for that. – Varun Sharma Nov 23 '16 at 23:46

1 Answers1

1
^[a-zA-Z0-9]+(?:\.?[\w!#$%&'*+/=?^`{|}~\-]+)*@[a-zA-Z0-9](?:\.?[\w\-]+)+\.[A-Za-z0-9]+$

No .. and at least 1 . and 1 @.
Also starts/ends with letters/numbers.

The ^ (start) and $ (end) were just added to match a whole string, not just a substring. But you could replace those by a word boundary \b.

An alternative where the special characters aren't hardcoded:

^(?!.*[.]{2})[a-zA-Z0-9][^@\s]*?@[a-zA-Z0-9][^@\s]*?\.[A-Za-z0-9]+$
LukStorms
  • 28,916
  • 5
  • 31
  • 45