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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lolol
  • 4,287
  • 3
  • 35
  • 54
  • 2
    You must always specify what language you are using your regex from. It's even written in the tag. – xanatos Oct 03 '11 at 14:16
  • Pretty please, don't pollute the Internet with yet another broken email address "validator". Please read and understand http://www.regular-expressions.info/email.html for a start. – tripleee Oct 03 '11 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 03 '11 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 03 '11 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 03 '11 at 15:28

2 Answers2

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.

xanatos
  • 109,618
  • 12
  • 197
  • 280
1

Be careful. This is a relatively weak expression and matches even user@server.com.net.org. 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
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sidyll
  • 57,726
  • 14
  • 108
  • 151