Java 多行gnu正则表达式

Java 多行gnu正则表达式,java,regex,gnu,Java,Regex,Gnu,我想在新行后面匹配一个表达式。我使用gnu.regexp和flag REG_MULTILINE。 这是我要检查的字符串 Hello Dude 我的方法是使用这个正则表达式来匹配 String strRegExp = "^[Dd][Uu][Dd]"; 但它不起作用。看不出问题出在哪里。 我在一个简单的单元测试中运行所有这些: @Test public void testREMatchAtStartOfNewLine() throws Exception { String strRegE

我想在新行后面匹配一个表达式。我使用gnu.regexp和flag REG_MULTILINE。 这是我要检查的字符串

Hello
Dude
我的方法是使用这个正则表达式来匹配

String strRegExp = "^[Dd][Uu][Dd]";
但它不起作用。看不出问题出在哪里。 我在一个简单的单元测试中运行所有这些:

@Test
public void testREMatchAtStartOfNewLine()
throws Exception {
    String strRegExp = "^[Dd][Uu][Dd]";
    int flags = RE.REG_MULTILINE;
    String strText ="Hello\nDude";
    RE re = new RE(strRegExp, flags, RESyntax.RE_SYNTAX_PERL5);
    REMatch match = re.getMatch (strText);
    String strResult = "";
    if (match != null) {
        strResult = match.substituteInto ("$0");
    }
    assertEquals("Match at start of new line ", "Dud", strResult);   // FAILS
}
提前谢谢。 编辑: 为了澄清,我使用了以下导入:

import gnu.regexp.RE;
import gnu.regexp.REMatch;
import gnu.regexp.RESyntax;

我不确定您使用的是什么正则表达式,但以下内容对我有用:

import java.util.regex.Matcher;
import java.util.regex.Pattern;
...

@Test
public void testREMatchAtStartOfNewLine() {
    String strRegExp = ".*\n([Dd][Uu][Dd]).*";
    Pattern pattern = Pattern.compile(strRegExp);
    String strText = "Hello\nDude";
    Matcher matcher = pattern.matcher(strText);
    assertTrue(matcher.matches());
    assertEquals("Match at start of new line ", "Dud", matcher.group(1)); // WINS
}

请注意模式前面和末尾的
“*”
。默认情况下,Java正则表达式需要匹配整个字符串,因此这些字符串是必需的。

鉴于主页的下载链接已断开,您使用GNU正则表达式库的原因是什么?:)

无论如何,使用
RESyntax.RE\u SYNTAX\u PERL5
时,似乎需要
\r\n
作为行分隔符。将
\n
替换为
\r\n
似乎有效