e444. matching across line boundaries in a regular expression.txt

来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 18 行

TXT
18
字号
By default, the any-character matcher (.) does not match line termination characters such as \n and \r. To allow dot (.) to match line termination characters, the pattern should be compiled with the dotall flag enabled. 
It is also possible to enable dotall mode within a pattern using the inline modifier (?s). For example, dotall mode is enabled in the pattern (?s)a.*b. Dotall mode can be disabled using (?-s). 

    CharSequence inputStr = "abc\ndef";
    String patternStr = ".*c.+d.*";
    
    // Compile with dotall enabled
    Pattern pattern = Pattern.compile(patternStr, Pattern.DOTALL);
    Matcher matcher = pattern.matcher(inputStr);
    boolean matchFound = matcher.matches();    // true
    
    // Use an inline modifier to enable dotall mode
    matchFound = pattern.matches(".*c.+d.*", "abc\r\ndef");     // false
    matchFound = pattern.matches("(?s).*c.+d.*", "abc\r\ndef"); // true

 Related Examples 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?