What is the difference between php reverse reference 1 and $1?

Source: Internet
Author: User
Tags expression engine
What is the difference between php reverse reference 1 and $1? Php regular expression. someone will see it using \ 1.
Use $1 later

What are the differences between the two? Which one is generally used?


Reply to discussion (solution)

The rule string can only be shown as \ 1
Because $ indicates the end of a row, \ $1 is prone to ambiguity.

For later use, there is no difference
Perl uses $1 to access the matching group.


In the regular expression, only \ 1 can be used, and in the replacement string, both can be used.

Preg_replace can contain the backward reference \ n or (php 4.0.4 or above) $ n, which is preferred in syntax. Each such reference replaces the text captured by the nth capturing sub-group that is matched. N can be 0-99, \ 0 and $0 represent the complete pattern matching text. The serial number counting method of the captured sub-group is as follows: left parentheses of the captured sub-group start from left to right and start from 1. To use a backslash in replacement, you must use four, after the regular expression engine is used, it is considered as a backslash of the original article ).

When it works in the replacement mode and the back reference is followed by another number (for example, adding an original number after a matching mode ), you cannot use a syntax like \ 1 to describe backward references. For example, \ 11 will make preg_replace () unable to understand what you want is a \ 1 back reference followed by an original article 1, or a \ 11 post is not followed by anything. In this case, the solution is \ $ {1} 1. This creates an independent $1 back reference and an independent original 1.

Reference from php manual preg_replace

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.