我有一个简单的程序,我正在尝试使用正则表达式将日期与字符串匹配。这是我的程序
Pattern r = Pattern.compile("((?:january|february|march|april|may|june|july|august|september|october|november|december)\\s*\\d{1,2}\\,\\s*\\d{4})\\s*\\|\\s*UPDATED", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
String textRegion = "<span>India Today Online</span>"
+ " New Delhi, October 28, 2013 | UPDATED 11:42 IST "
+ "</div>";
Matcher m = r.matcher(textRegion);
if (m.find()) {
System.out.println("Matched");
} else {
System.out.println("Not Matched");
}
但是当我运行这个程序时,它显示“不匹配”但是当我在http://gskinner.com/RegExr/上尝试相同的东西时,它显示了匹配的字符串。请帮助我理解我错在哪里。
我面临着有趣的问题。我的程序有两个部分,第一部分显示不匹配,第二部分显示匹配。在第一部分中,我从文本文件中复制了一个字符串。我无法理解这个问题。
String textRegion = "<span>India Today Online</span>"
+ " New Delhi, October 28, 2013 | UPDATED IST"
+ "</div>";
Pattern r = Pattern.compile("((?:january|february|march|april|may|june|july|august|september|october|november|december)\\s*\\d{1,2}\\,\\s*\\d{4})\\s*\\|\\s*UPDATED", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
System.out.println(textRegion);
Matcher m = r.matcher(textRegion);
if (m.find()) {
System.out.println("Matched");
} else {
System.out.println("Not Matched");
}
Pattern r1 = Pattern.compile("((?:january|february|march|april|may|june|july|august|september|october|november|december)\\s*\\d{1,2}\\,\\s*\\d{4})\\s*\\|\\s*UPDATED", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
String textRegion1 = "<span>India Today Online</span>"
+ " New Delhi, October 28, 2013 | UPDATED IST"
+ "</div>";
Matcher m1 = r1.matcher(textRegion1);
if (m1.find()) {
System.out.println("Matched");
} else {
System.out.println("Not Matched");
}
有趣的部分是如果你从这里复制程序它对我来说也很好。