📄 e444. matching across line boundaries in a regular expression.txt
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -