something Line One
something Line two
Last annotated on December 31, 2014
I don't care what is in between. Anything can go here. Any number of lines
I don't care what is in between. Anything can go here. Any number of lines
Something Line three
something line four
Last annotated on November 15, 2014
我想写一个匹配
的正则表达式something Line One
something Line two
Last annotated on December 31, 2014
和
Something Line three
something line four
Last annotated on November 15, 2014
我写的下面的正则表达式适用于Sublimetex,但是同样的正则表达式对java不起作用。
.*\n.*\nLast annotated on \w* \d{2}, \d{4}
以下是与模式不匹配的java代码。
String regex = ".*\\n.*\\nLast annotated on \\w* \\d{2}, \\d{4}";
Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
答案 0 :(得分:0)
您需要使用带有find
函数的Pattern和Matcher类。
String s = "something Line One\n" +
"something Line two \n" +
"Last annotated on December 31, 2014\n" +
"\n" +
"I don't care what is in between. Anything can go here. Any number of lines\n" +
"I don't care what is in between. Anything can go here. Any number of lines\n" +
"\n" +
"Something Line three\n" +
"something line four\n" +
"Last annotated on November 15, 2014";
Matcher m = Pattern.compile(".*[\\r\\n]+.*[\\r\\n]+Last annotated on \\w* \\d{2}, \\d{4}").matcher(s);
while(m.find())
{
System.out.println(m.group());
}
输出:
something Line One
something Line two
Last annotated on December 31, 2014
Something Line three
something line four
Last annotated on November 15, 2014