Reputation:
I am trying to validate email in java. Below is the code:
String mail = "[email protected]";
String regex = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
if(mail.matches(regex)){
System.out.println("valid email");
};
Now i want to restrict the length of Text1 and Text2 to 10 characters and length of Text3 to 5 characters.
I tried with this regex but did not work -
^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]{2,}+)*@[A-Za-z0-9]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$
How do i achieve this? Thanks.
Upvotes: 1
Views: 2534
Reputation: 1085
^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$
I have used this another approach.
Upvotes: 0
Reputation: 27516
Here's the regex:
String regex = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]{0,10})*@[A-Za-z0-9]+(\\.[A-Za-z0-9]{0,10})*(\\.[A-Za-z]{0,5})$";
This doesn't count dots as a character, uses {n, m}
syntax, it limits the number of characters to be between n and m occurrences.
Upvotes: 1