Matching Newlines in Text
Problem
You need to match newlines in text.
Solution
Use \n or \r.
See also the flags constant RE.MATCH_MULTILINE, which makes
newlines match as beginning-of-line and end-of-line
(^ and $).
Discussion
While line-oriented tools from
Unix such as sed
and grep match regular expressions one line at a
time, not all tools do. The
sam
text editor from Bell Laboratories was
the first interactive tool I know of to allow
multiline regular expressions; the
Perl scripting language followed shortly. In our API, the newline
character by default has no special significance. The
BufferedReader method readLine( ) normally strips out whichever newline characters it
finds. If you read in gobs of characters using some method other than
readLine( ), you may have \n in
your text string. Since it’s just an ordinary character, you
can match it with .* or similar multipliers, and,
if you want to know exactly where it is, \n or
\r in the pattern will match it as well. In other
words, to this API, a newline character is just another character
with no special significance. You can recognize a newline either by
the metacharacter \n, or you could also refer to
it by its numerical value, \u000a.
import org.apache.regexp.*; /** * Show line ending matching using RE class. */ public class NLMatch { public static void main(String[] argv) throws RESyntaxException { String input = "I dream of engines\nmore engines, all day long"; System.out.println("INPUT: " + input); System.out.println( ...