Question

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. ss2@dad.2om p2@   2@2.2 fad2@yahoo.com 22@yahoo.com fad@yahoo.com");
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.

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top