Miscellaneous about perl Regular Expressions

Source: Internet
Author: User

$ 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.

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.