![]() |
The Java Developers Almanac 1.4Order this book from Amazon. |
e444. Matching Across Line Boundaries in a Regular ExpressionBy 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 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
e442. Implementing a FilterReader to Filter Lines Based on a Regular Expression e443. Matching Line Boundaries in a Regular Expression e445. Reading Lines from a String Using a Regular Expression e446. Removing Line Termination Characters from a String
© 2002 Addison-Wesley. |