4.8.3.2.1 Pattern Matching Over Multi-Line Strings With No Modifiers
4.8.3.2.1 Pattern Matching Over Multi-Line Strings With No Modifiers
Things can become complicated when we talk about pattern matching over multiple lines of text. We get to the complications later. Let us look at the simplest case of multi-line strings first.
It is possible to match over multi-line strings without using any modifiers at all. Unless we want to change the behavior of the anchors ^ and $, or want the dot (.) character to match \n (which it normally does not), pattern matching over multiple lines of text can be done the normal way. Here is an example program. We want to find out the first instance of two integers in sequence and add them and print the result.
Program 4.31
#!/usr/bin/perl
$textAndNumbers = "\t\n A \t B \n\n\n12\t\n\t\t13 \t \nC\t20\n\t31\t40\nD";
($first, $second) =
$textAndNumbers =~ m/(\d+)\s+(\d+)/;
print $first + $second, "\n";
Here, the fact that there are embedded \n’s in the target string does not matter at all. The \s+ subpattern inside the regular expression matches one or more \n’s and other white-space characters. The program prints 25 as the result of adding the first two consecutive numbers it finds
