Reputation: 66
I have to make regex expression for e-mail. It is allowed to have letters or numbers only before the @ symbol, optionally only the dot that can not be on the start. After @ symbol it is allowed to have letters or numbers only, exactly one dot and after dot at least 2 characters.
That's my expression
/([a-zA-Z\d*])+(\.?)([a-zA-Z\d*])*@{1}([a-zA-z\d*])+(\.){1}([a-zA-Z\d*]){2,}/
Whole email should have between 10 and 30 characters, and I don't know how to do that.
Thanks
Upvotes: 1
Views: 1179
Reputation: 4302
^(?=^.{10,30}$)(?=^[A-Za-z0-9])[A-Za-z0-9\.]+@[A-Za-z0-9]+\.[A-Za-z]{2,}$
https://regex101.com/r/cBACF2/10
Should be between 10 to 30 (?=^.{10,30}$)
Start with letters & numbers (?=^[A-Za-z0-9])
contains letters & numbers and dot [A-Za-z0-9\.]
at least two letter after after last dot [A-Za-z]{2,}
Upvotes: 0
Reputation: 18950
Actually, you can prefix your pattern with a look-ahead and a quantifier to match between 10 and 30 characters: (?=^.{10,30}$)
Then, your pattern looks like this:
(?=^.{10,30}$)([a-zA-Z\d*])+(\.?)([a-zA-Z\d*])*@{1}([a-zA-z\d*])+(\.){1}([a-zA-Z\d*]){2,}
Upvotes: 1
Reputation: 5642
The syntax for a range of allowed repeats is {n,m}
. You wrote {1}
meaning "exactly one" which is pointless. {10,30}
is the range you are looking for.
Also, know the escape code for "letters". \w
is a "word character", which is the same as [a-zA-Z0-9_]
. And why is there a '*' in the character range?
So the problem is that you have pieces which can end up being of various lengths, and need to check the total when done, right?
In Perl you can include code as a assertion. So include (?{ length($&) <= 30 })
as the final assertion.
Upvotes: 0