Maximum Regular Expression Email Authentication Length

I find this regular expression to check email.

[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})

I want the max length for email to be 20 characters, so I change it to:

([_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})){0,20}

but when I entered more than 20 characters, it takes! also i used

^([_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})){0,20}$ 

but it did not work correctly. I want to use it in java code

+3
source share
1 answer

You can't just add {0,20}to the entire regex, as that would mean from 0 to 20 the appearance of each email address.

You can use it like this using lookahead to ensure the length:

^(?=.{1,20}$)[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$
+1
source

Source: https://habr.com/ru/post/1685082/


All Articles