![]() |
The Java Developers Almanac 1.4Order this book from Amazon. |
e443. Matching Line Boundaries in a Regular ExpressionBy default, the beginning-of-line matcher (^) and end-of-line matcher ($) do not match at line boundaries. They match the beginning and end of the entire input sequence. For example, the pattern^a matches
abc but does not match def\nabc . To enable ^ and
$ to match line boundaries, the pattern should be compiled with
the multiline flag enabled.
It is also possible to enable multiline mode within a pattern
using the inline modifier CharSequence inputStr = "abc\ndef"; String patternStr = "abc$"; // Compile with multiline enabled Pattern pattern = Pattern.compile(patternStr, Pattern.MULTILINE); Matcher matcher = pattern.matcher(inputStr); boolean matchFound = matcher.find(); // true // Use an inline modifier to enable multiline mode matchFound = pattern.matches(".*abc$.*", "abc\r\ndef"); // false matchFound = pattern.matches("(?m).*abc$.*", "abc\r\ndef"); // true
e442. Implementing a FilterReader to Filter Lines Based on a Regular Expression e444. Matching Across 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. |