How can I solve regular expression reverse references in php?

Source: Internet
Author: User
How can I solve regular expression reverse references 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, 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 = "abcd 1234reo 5678 ";

// 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 don't change it, add a bracket directly and write it by myself ,?
);


Reply to discussion (solution)

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

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


Yes. can you explain it?

What should I do if I reference some of them?
For example, if "1234" matches, reference the number in it, and add the brackets [1234]?




I wrote the regular expression in the array. you cannot use this method. it must be $ {0} in the array.

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




I know, it's because your regular expression method is different from my writing method, but why? I wrote nothing wrong?


I am:/(\ d) {4}/I
You are:/(\ d {4 })/

Are these two actually different? Solve ???????????

/(\ 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 ?? Indicates that the content of the first pair of parentheses is reversed, followed by a 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)

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

/(\ 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 ?? Indicates that the content of the first pair of parentheses is reversed, followed by a 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)

I am:/(\ d) {4}/I
You are:/(\ d {4 })/

Are these two actually different? Solve ???????????

You have figured it out. thank you.

Subexpression
Capture
Reverse reference

Thank you!

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.