在 Java 正则表达式中如何匹配换行符

In Java regex how to match newline character

知道为什么这个 Java 测试用例失败了吗?

@Test
public void newlineParse() throws Exception {
    Pattern pat = Pattern.compile("a.*b", Pattern.MULTILINE);
    assertTrue(pat.matcher("a\nb").find());
}

我认为问题在于 Pattern.MULTILNE 不正确。对于特定示例,它应该是 Pattern.DOTALL(或在表达式中嵌入 ?s)。

MULTILINE:

Enables multiline mode.
In multiline mode the expressions ^ and $ match just after or just before, respectively, a line terminator or the end of the input sequence. By default these expressions only match at the beginning and the end of the entire input sequence.
Multiline mode can also be enabled via the embedded flag expression (?m).

DOTALL:

In dotall mode, the expression . matches any character, including a line terminator. By default this expression does not match line terminators.

A working example using DOTALL