I tried this code:
public static void main(String[] args) throws Exception {
String regexp = "[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)*(\\.)[a-zA-Z]+";
String text1 = "my.name-here@my.domain-here.ext";
String text2 = "my.name-here@m@y.domain-here.ext";
String text3 = "@domain-here.ext";
String text4 = "my.name-here@";
String text5 = "my.name-here@domain-here";
String text6 = ".my.name-here@my.domain-here.ext";
String text7 = "my.name-here.@my.domain-here.ext";
String text8 = "my.name-here@.my.domain-here.ext";
String text9 = "my.name-here@my.domain-here.ext.";
String text10 = "my.na me-here@my.domain-here.ext";
String text11 = "my.name-here@my.dom ain-here.ext";
String text12 = "my..name-here@my.domain-here.ext";
String text13 = "my.name-here@my..domain-here.ext";
RE re = new RE(regexp);
System.out.println(re.match(text1));
System.out.println(re.match(text2));
System.out.println(re.match(text3));
System.out.println(re.match(text4));
System.out.println(re.match(text5));
System.out.println(re.match(text6));
System.out.println(re.match(text7));
System.out.println(re.match(text8));
System.out.println(re.match(text9));
System.out.println(re.match(text10));
System.out.println(re.match(text11));
System.out.println(re.match(text12));
System.out.println(re.match(text13));
System.out.println(Pattern.matches(regexp, text1));
System.out.println(Pattern.matches(regexp, text2));
System.out.println(Pattern.matches(regexp, text3));
System.out.println(Pattern.matches(regexp, text4));
System.out.println(Pattern.matches(regexp, text5));
System.out.println(Pattern.matches(regexp, text6));
System.out.println(Pattern.matches(regexp, text7));
System.out.println(Pattern.matches(regexp, text8));
System.out.println(Pattern.matches(regexp, text9));
System.out.println(Pattern.matches(regexp, text10));
System.out.println(Pattern.matches(regexp, text11));
System.out.println(Pattern.matches(regexp, text12));
System.out.println(Pattern.matches(regexp, text13));
}
Only the first one must be right but… org.apache.regexp.RE does something wrong. Any solution? Thanks a lot. I have to do it with org.apache.regexp.RE obligatory.
Update: Pattern do matches right (all false except the first one), RE say someone String are true but they isn’t.
Your expression does not state that it must match the entire input and thus I assume within
my.name-here@m@y.domain-here.extApache Regexp matches them@y.domain-here.extpart (although I don’t know Apache RegexP that well and it is retired, btw).Wrap your regex with ^ and $ to make it match the entire input.
From the JavaDoc on
Matcher#matches()(Pattern.matches(...)calls that method as you can see from its JavaDoc):Edit
I just tested your expression in the RegexP applet and it seems like you have to escape the literal
-in your character classes (which is good practice anyways). This expression seems to work in RegexP:Btw, you might want to add non-capturing groups to optimize the expression a bit, i.e. instead of
(\\.)you’d write(?:\\.)etc.