Php regular expression reverse reference-php Tutorial

Source: Internet
Author: User
Tags php regular expression expression engine
Php regular expression reverse reference problem? In theory, I know the following: & nbsp; 1 indicates that the value of the first subexpression is referenced; $ {1} & nbsp; I am ignorant. I can use it, but I cannot find the instructions in the relevant documents. please instruct the php regular expression to reverse reference the problem?
Theoretically:

What I know about the reverse reference subexpression is:

\ 1
The value that references the first subexpression.

Later, I did not intend to see someone referencing it like this: $ {1}. I am ignorant and can use it, but I cannot find the instructions in the relevant documents.

Share:


------ Solution --------------------
$1 is used in js.
------ Solution --------------------
Http://cn2.php.net/manual/zh/function.preg-replace.php has instructions:

String or string array to be replaced. If this parameter is a string and pattern is an array, all modes use this string for replacement. If both pattern and replacement are arrays, each pattern is replaced with the corresponding elements in replacement. If replacement contains fewer elements than pattern, the extra pattern is replaced with a null string.

Replacement can contain 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.

When the e modifier is used, this function will escape some characters (I .e., ', ", \, and NULL) and then carry out backward reference replacement. After completing these steps, make sure that there are no syntax errors caused by single or double quotation marks (for example, 'strlen (\ '$1 \') after the back-to-reference resolution is complete \') + strlen ("$2 ")'). Make sure the PHP string syntax is followed and the eval syntax is followed. After replacement, the engine evaluates the result string as the php code using the eval method and uses the returned value as the final string to be replaced.

------ Solution --------------------
Reference \

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.