Regular expression doesn't match empty string in multiline mode (Java)

后端 未结 3 722
温柔的废话
温柔的废话 2021-01-04 01:31

I just observed this behavior;

Pattern p1 = Pattern.compile(\"^$\");
Matcher m1 = p1.matcher(\"\");
System.out.println(m1.matches()); /* true */

Pattern p2         


        
3条回答
  •  梦谈多话
    2021-01-04 02:15

    If MULTILINE mode is activated then ^ matches at the beginning of input and after any line terminator except at the end of input.

    Since you are at the end of input, ^ can't match in multiline mode.

    This is surprising, even disgusting, but nevertheless according to its documentation.

提交回复
热议问题