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!