Some miscellaneous _perl about Perl regular expressions

Source: Internet
Author: User

$ can also match \ n

See Introduction to Perl Language, page 132, note 6

Can 1/^.*$/match "\ n"? Yes! Because the $ can match not only the end of the line, but also \ n
Can 2/^.*$/match "b\n"? Can be!. Can b match. \ n Match $
Can 3/^.*$/match "\NB"? No! Why? Because by default,. Cannot match \ n, change the pattern to/^.*$/s on it, and/s. can match any character, including \ n

Multiple-line match/M

Look at an example of this code output: Hello

Copy Code code as follows:

My $text = "Hello, World,\nhello zdd,\nhello Autumn";
while ($text =~/^hello/g) {
Print "hello\n"
}

A little change, plus the/m option

Copy Code code as follows:

My $text = "Hello, World,\nhello zdd,\nhello Autumn";
while ($text =~/^hello/mg) {
Print "hello\n"
}

Now the output becomes
Hello
Hello
Hello

Annotations:
By default, ^ and $ match the beginning and end of the entire string, but after plus/m, the ^ and $ match the beginning and end of each line. That is, because there is a newline character in the string \ n, the/m option causes ^$ to match the beginning and end of each line.
If there are no newline characters in the string, the/m option does not work.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.