$ Can also match \ n
For more information about Perl, see page 132. Note 6.
1/^. * $/can it match "\ n? Yes! Because $ not only matches the end of a row, but also matches \ n
2/^. * $/can it match "B \ n? Yes !. B matches. \ n matches $
3/^. * $/can it match "\ nb? No! Why? By default, \ n cannot be matched. Change the pattern to/^. * $/s./s indicates that. can match any character, including \ n.
Multi-row matching/m
Let's look at an example. The output of this Code is hello.
Copy codeThe Code is as follows: my $ text = "hello, world, \ nhello zdd, \ nhello autumn ";
While ($ text = ~ /^ Hello/g ){
Print "hello \ n"
}
Change the value slightly, and add the/m option.
Copy codeThe Code is as follows: my $ text = "hello, world, \ nhello zdd, \ nhello autumn ";
While ($ text = ~ /^ Hello/mg ){
Print "hello \ n"
}
Now the output is changed
Hello
Hello
Hello
Note:
By default, ^ and $ match the beginning and end of the entire string, but after adding/m, ^ and $ match the beginning and end of each line. That is to say, because there is a linefeed \ n in the string, the/m option makes ^ $ match the beginning and end of each line.
If there is no line break in the string, the/m option does not work.