What I'm trying to do is making a valid mail id using regular expressions, from a given string. This is my code:
Pattern pat3 = Pattern.compile("[(a-z)+][(a-z\\d)]+{3,}\\@[(a-z)+]\\.[(a-z)+]");
Matcher mat3 = pat3.matcher("dasdsa@2 @ada. [email protected] p2@ [email protected] [email protected] [email protected] [email protected]");
System.out.println(mat3.pattern() + " ");
while(mat3.find()){
System.out.println("Position: " + mat3.start() + " ");
}
The problem is nothing is printed out. What I want to print, and what I really expect to print, but it doesn't, is: 39, 67.
Can someone explain me, why \\.
doesn't work? Before putting \\.
my regex was working fine till that point.
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: [email protected]
Position: 67 --- Match: [email protected]
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