开发者

Java RegEx and linebreaks - bug or expected behavior?

开发者 https://www.devze.com 2023-02-21 12:17 出处:网络
I\'m trying to interpret a multiline string with RegEx and just found that matching will fail if the string contains newline characters. I\'m NOT using using MULTILINE mode, because I\'m not using anc

I'm trying to interpret a multiline string with RegEx and just found that matching will fail if the string contains newline characters. I'm NOT using using MULTILINE mode, because I'm not using anchors. According to API docs:

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.

In short: it clearly says that this flag only changes how anchors work and says nothing like "when your string contains a newline you should definitely use this".

public static void main(String[] args) {
    Pattern p = Pattern.compile(".*");

    Matcher m1 = p.matcher("Hello");
    System.out.println("m1: " + m1.matches());开发者_StackOverflow    // true

    Matcher m2 = p.matcher("Hello\r\n");
    System.out.println("m2: " + m2.matches());    // false
}

So is this really a bug, or I just missed some docs? Or JAVA uses a dialect of RegEx where my pattern fails? I'm using jdk1.6.0_21.


From the Pattern docs:

The regular expression . matches any character except a line terminator unless the DOTALL flag is specified.

So you need to specify the DOTALL flag if you want m2.matches() to be true.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号