Reverse extraction of regular expressions in php

Source: Internet
Author: User
How can I solve regular expression reverse references in php? This post was last edited by u010572351 from 2013-06-0617: 46: 46. the regular expression is used to add a bracket to the four consecutive numbers, and the numbers remain unchanged. What the teacher said is reverse capture. it seems that {1} is used. I tried it and couldn't do it. what I said on the Internet is \ 1. is there a reaction, but it doesn't solve the regular expression reverse reference in php?
This post was last edited by u010572351 at 17:46:46 on. The regular expression is used to add a bracket to the four consecutive digits of the match, and the number remains unchanged.

What the teacher said is reverse capture. it seems that {1} is used. I tried it and couldn't do it. what I said online is \ 1. the response was correct, then I changed the \ 0 written below and captured it in reverse direction. but I checked it online. it seems that I did not write it like this ,,,,, but I did capture four numbers. why ??? Also, what does array-based regular expressions mean?

$ A = "abcd1234reo5678 ";

// Below we use the regular expression in php to process the above content

$ Myreg = array (
"/(\ D) {4}/I"
);

$ Con = array (
'[\ 0]' // capture the number. if you do not change it, add a bracket directly. I will write it myself.
);

Share:
------ Solution --------------------
 $ S = 'wqe2313jyu6y6546iuoi ';
$ S = preg_replace ('/(\ d {4})/', '{$ {1}', $ s );
Print ($ s );
?>

Running result:
Wqe {2313} jyu6y {6546} iuoi

------ Solution --------------------
/(\ D {4 })/
[$1]

The number referenced in the reverse direction indicates the number of pairs of parentheses in the regular expression from left to right. Note (?...) Not included

Used only when there is ambiguity {}
For example, if you need to add a number after the reverse reference, it will produce ambiguity. you need to write $ {1} 1 -- to indicate the reverse reference of the first pair of parentheses, followed by the number 1
However, $11 references the content of the 11th pair of parentheses in reverse direction (if there are not so many parentheses, it is null)

------ Solution --------------------
/((? : 123) 456 (789 ))/
$1 get 123456789
$2 get 789

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.