Make your pattern as the following :
[a-z]+[a-z\\d]+{3,}\\@[a-z]+\\.[a-z]+
So, the code will be :
Pattern pat3 = Pattern.compile("[a-z]+[a-z\\d]+{3,}\\@[a-z]+\\.[a-z]+");
// Your Code
while(mat3.find()){
System.out.println("Position: " + mat3.start() + " --- Match: " + mat3.group());
}
This will give the following result :
Pattern :: [a-z]+[a-z\d]+{3,}\@[a-z]+\.[a-z]+
Position: 39 --- Match: fad2@yahoo.com
Position: 67 --- Match: fad@yahoo.com
Explanation:
You have put the pattern as
[(a-z)+][(a-z\\d)]+{3,}\\@[(a-z)+]\\.[(a-z)+]
the character set, [(a-z)+]
will not match one or more repetition of lower-case alphabet. It will match only one occurrence of any of these : (
, a-z
, )
, +
to match one or more repetition of lower-case alphabets, the character set should be like [a-z]+
So if you remove the \\.
part from your pattern , and
while(mat3.find()){
System.out.println("Position: " + mat3.start() + " --- Match: " + mat3.group());
}
will give :
Pattern :: [(a-z)+][(a-z\d)]+{3,}\@[(a-z)+][(a-z)+]
Position: 15 --- Match: ss2@da // not ss2@dad
Position: 39 --- Match: fad2@ya // not fad2@yahoo
Position: 67 --- Match: fad@ya // not fad@yahoo