0

I'm having loads of trouble trying to fulfill the requirements of this regex as stated below.

^[A-Za-z0-9]{1,}|^[A-Za-z0-9]{1,}[\-\_]\.[a-zA-Z]{2,}@[A-Za-z0-9][\-\_]{5,}\.[a-zA-Z]{2,}$

"Your Email and Re-enter email must not be blank and must have the form acct@domain where:

  • acct

    1. 1 or more characters
    2. Composed of only upper or lowercase alphabetic characters, numeric characters, dashes, periods, underscores and hyphens
    3. Contains no embedded blanks.
    4. Cannot start or end with an underscore, dash, period or hyphen.
    5. There must be at least two letters before and after every period.

  • domain
    1. 5 or more characters
    2. Composed of only upper or lowercase alphabetic characters, numeric characters, dashes, periods, and hyphens, underscores
    3. Contains no embedded blanks
    4. Must have at least one period, and cannot start or end with an underscore, dash, period or hyphen.
    5. There must be at least two letters before and after every period.

  • The email address must contain an @ between acct and domain

Sample valid email addresses:

[email protected], [email protected]_3.com

Sample invalid email addresses:

b@b, [email protected], [email protected]!c

Both email addresses must be the same"

4
  • Is this homework? Underscore _ is not a valid character in a domain name. Commented Nov 18, 2014 at 4:32
  • Yeah, but according to the requirements it is... So frustrated!!! Commented Nov 18, 2014 at 4:53
  • 1
    My advice about regular expressions and email: davidcel.is/blog/2012/09/06/… Commented Nov 18, 2014 at 4:55
  • 1
    I would avoid using regex to validate emails. That said, if you have to, you should look into debuggex.com. It will help you make a good regex and unit test it for thoroughness. Commented Nov 18, 2014 at 4:57

2 Answers 2

3

Below is the regex which can validate all your criteria and I hope it is also more efficient.

^(?![\W_])((?:([\w-]{2,})\.?){1,})(?<![\W_])@(?![\W_])(?=[\w.-]{5,})(?=.+\..+)(?1)(?<![\W_])$

Look at the regex demo for detailed overview.

Sign up to request clarification or add additional context in comments.

1 Comment

Just wanted to thank you for this as it was the closest, and used a new world of regular expressions for me. Through thorough research, I was able to understand how this worked and modified it to suit the requirements. The tools regex101 and debuggex are amazing as well and I wouldn't have discovered them if it wasn't for the help of you guys.
1

Your regex can be dramatically simplified (probably even more so than this example), but then since your criteria is actually a little more sophisticated than your examples let on:

^[a-z]([\w-]*[a-z]|[\w-.]*[a-z]{2,}|[a-z])*@[a-z]([\w-]*[a-z]|[\w-.]*[a-z]{2,}|[a-z]){4,}?\.[a-z]{2,}$

Example: http://regex101.com/r/gE4wK2/2 (note the case-insensitive option too)

Where the larger problem lies, however, is the discrepancies in the valid samples you've provided.

Account:

  • 1 or more characters
    • [a-z] ensures there is at least one character exists in the account
  • composed of only upper or lowercase letters, numbers, periods, underscores and hyphens, and contains no whitespace
    • ([\w-]*) ensures that you can have any letter or hyphen
  • cannot start or end with an underscore, hyphen, or period
    • ([a-z]|[a-z])* ensures that the last character, if it exists, is a letter
  • there must be at least two letters before and after every period
    • [\w-.]*[a-z]{2,} ensures that if there's a period, there must be two letters following it.

Already you can see that your examples like [email protected] shouldn't match.

@ symbol must exist between account and domain

  • That's easy: there's an @ literal in the expression.

Domain:

  • 5 or more characters
    • (...big long expression...){4,} ensures that there are 4 additional characters after the initial domain letter (total of 5)
  • composed of only upper or lowercase alphabetic characters, numeric characters, hyphens, periods, and underscores and contains no white space
    • ([\w-]*[a-z]) nails that one
  • must have at least one period
    • There's a period literal in the expression \.
  • cannot start or end with an underscore, hyphen, or period
    • [a-z] ensures the first character of the domain is a letter
  • there must be at least two letters before and after every period
    • [\w-.]*[a-z]{2,} if there's a period in the string, 2 letters must follow
    • [a-z]{2,}$ ensures that there are at least two letters following the domain period

All of this however, and I still advocate against complex regular expressions to validate email addresses: read http://davidcel.is/blog/2012/09/06/stop-validating-email-addresses-with-regex/

In the end, check for a super basic email address like ^.*?@.*?\..*?$ and just send the thing!

2 Comments

You have NO IDEA how happy I am... Thank you all for the amazing resources as well that will help contribute to successful regex construction!!!! those websites are amazing!
You're welcome ;) Do not go do not go gentle into that good black hole of regex email validation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.