我无法在我的程序中实现此功能。我不知道该怎么办..
目前我有这个:
String content = message.getContent().toString();
Pattern p = Pattern.compile("(?:Feb)");
Matcher m = p.matcher(content);
if (m.matches())
{
System.out.print(m.group(1));
}
else
{
System.out.print("no matches");
}
System.out.println(content); //this will just print all the content.
然而,这并没有返回任何东西..我尝试过其他正则表达式方法无济于事。在控制台中,我可以看到打印的整个邮件。第四行包含一个我想要抓住并在其他地方使用的单词。它可能是Jan,Feb,Mar,Apr,May,Jun,Jul等任何一个月......我希望我的代码告诉我它是哪一个。
任何人都知道如何做到这一点? (ps。我的代码可能完全错误!)也许我可以使用javax.search.BodyTerm,但这似乎只返回true / false ..
电子邮件示例:
您好, 拉拉拉拉 我想要下面的月份。 2014年3月14日,星期五 再见。
谢谢!
答案 0 :(得分:0)
而不是这一行:
if (m.matches()) { System.out.print(m.group(1)); }
您将需要:
if (m.find()) { System.out.print(m.group()); }
这是因为Matcher#matches()
方法仅在匹配完成输入时返回true。
此外,您没有捕获任何群组,因此您无法使用group(1)
只使用group()
。