0

I'm using the following regular expression to validate emails:

^\w+([-.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$

Now I want to modify this regular expression to validate the email length using {5,100}. How can I do that?

5
  • 2
    You must always specify what language you are using your regex from. It's even written in the tag.
    – xanatos
    Oct 3, 2011 at 14:16
  • Pretty please, don't pollute the Internet with yet another broken email address "validator". Please read and understand regular-expressions.info/email.html for a start.
    – tripleee
    Oct 3, 2011 at 14:38
  • As an additional note to what @tripleee wrote, please don't use the last one. It's based on a "closed" list of TLD, but now anyone rich enough can buy a TLD. Someone could even buy a nospam TLD :-)
    – xanatos
    Oct 3, 2011 at 14:47
  • Yes, seconded. It cautions against using the proposed regex blindly, but perhaps it should be clearer about "don't do this at home, at all".
    – tripleee
    Oct 3, 2011 at 14:51
  • Xanatos: tags are ok now. triplee: the expression fits my needs, read a manual about "over compensating my penis size on the internet".
    – lolol
    Oct 3, 2011 at 15:28

2 Answers 2

2
^(?=.{5,100}$)\w+([-.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$

I'm using the zero-width lookahead. It won't consume characters, so you can then recheck them with your expression.

1
  • Thank you for the lookaround syntax. Best Regards.
    – lolol
    Oct 3, 2011 at 15:34
1

Be careful. This is a relatively weak expression and matches even [email protected]. Email regexes were already discussed in many other questions like this one.

Checking for the length of a string can generally be done within the language/tool you're using. This might be even faster in some cases. As an example (in Perl):

my $len = length $str;
if ($len < 5 || $len > 100) {
    # Something is wrong
}
1
  • I want to validade the len in the expression itself. Thx.
    – lolol
    Oct 3, 2011 at 15:19

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.