我想将以下正则表达式应用于字符串。它在 Grant Skinners Regexr 上运行良好,在http://www.regexplanet.com/advanced/java/index.html(区分大小写)上也运行良好,但 Java 不会接受它。它从来没有遇到过while循环。这是我的代码:
public static void main(String args[]) {
final String testString =
"lorem upsadsad asda 12esadas test@test.com asdlawaljkads test[at]test" +
"[dot]com test jasdsa meter";
final Pattern ptr =
Pattern.compile(
"^[A-Z0-9\\._%+-]+(@|\\s*\\[\\s*at\\s*\\]\\s*)[A-Z0-9\\.-]+" +
"(\\.|\\s*\\[\\s*dot\\s*\\]\\s*)[a-z]{2,6}$",
Pattern.CASE_INSENSITIVE);
try {
final Matcher mat = ptr.matcher(testString);
while (mat.find()) {
final String group1 = mat.group(1);
System.out.println(group1);
final String group2 = mat.group(2);
System.out.println(group2);
final String group3 = mat.group(3);
System.out.println(group3);
}
} catch (final Exception e) {
e.printStackTrace();
}
}