lolol
lolol

Reputation: 4390

Minimum and maximum length in a regular expression

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?

Upvotes: 0

Views: 7443

Answers (2)

sidyll
sidyll

Reputation: 59297

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
}

Upvotes: 1

xanatos
xanatos

Reputation: 111890

^(?=.{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.

Upvotes: 2

Related Questions