Issues related to "" escaping in regular expressions

Source: Internet
Author: User
Tags control characters
The problem of "\" escaping in regular expressions
Use regular in MySQL, if you encounter to match carriage return, write this: \\n
The book says because, MySQL is explaining a \, the regular expression is explaining a \.

So I think of PHP in the same way, about \ n problem. Then I did a test:
PHP Code
  
       Preg_match ("/\w{3}\\n\w{3}/", "aaa\naaa", $match);    Var_dump ($match);            


This can be the correct result, I can understand: a "\" let PHP explain, the other "\" is explained by the regular.

Then I changed the regular expression to: "/\w{3}\n\w{3}/"
This also came to the right result ~ ~ ~ I don't understand it.
Why in PHP, with a "\" can it? If according to the book, then this "\" is not interpreted by PHP? What about the regular one, no explanation?

------Solution--------------------
The regular inside is already matching the results you need. This is the Convention's symbol.
------Solution--------------------
\ n in double quotes, don't parse to wrap.
both \\n and \ n can match line breaks.
------Solution--------------------
discuss
It's wrong!
Is this:
That's like this string: $a = "AAA\\NAAA";
Must use "/\w{3}\\\\n\w{3}/" to match, this is why?
------Solution--------------------
\ n is a byte, the PRG needs to be such a byte, but it also thinks \ nthe two characters is a newline character, so the following are correct.

' \ n ' "\ n". "\\n"
------Solution--------------------
You understand the basics correctly. \ \ will be interpreted as ' \ ' by PHP, but \w PHP is still interpreted as \w
For control characters, if double quotation marks are used, \ n is interpreted by PHP as a newline, and the single quotation mark is still the character ' \ n ' in the regular expression, whether it is a newline or ' \ n ' can be matched to a newline character.
  • 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.