Imagine this problem, assuming that in order to facilitate the reading of long strings of numbers, you need to add a comma as a separate, how do you need to do?
2569836495 = "2,569,836,495
Regular expression matching is usually left to right, which results in the inability to use a method like [\d]{3} to solve the problem because the numbers are settled from the right
Use s/([\d]{3})/$1,/g to process the above numbers, only to get this result
2569836495 = "256,983,649,5
Obviously this is the wrong way of tagging
Fortunately pcre provides [sequential look], and [reverse look around] function
[=reg], for example (? =\d), will match the position on the right that matches the \d condition.
[Reverse look]: (? <=reg), for example (? <=\d), will match the position on the right that matches the \d condition.
Note: The two matching methods emphasize the word "position" because the surround view matches the pattern and only matches the position .
This example illustrates:
Existing string: "SELECT * from", Matching expression: (? =select)
The expression means matching the left position of the character that satisfies the condition in parentheses (here is Select)
The result will be this: "[I am the location, I was matched by]select * from"
If you don't understand it, continue modifying the expression: (? =select) SEL
At this point the red font portion is matched, "SELECT * from"
To parse: (? =select) matches "[I am the location, I am matched to]select * from", then starting from this position, need immediately followed by SEL, so "selECT * from" is matched.
And the reverse look is similar to the principle, but the matching method is from the left
Or use just the string: "SELECT * from", Match expression: (? <=select)
The expression means matching the right position of the character that satisfies the condition in parentheses (here is Select)
The result will be this: "select[I am the position, I was matched" * from "
If you don't understand it, continue modifying the expression: ECT (? <=select)
At this point the red font portion is matched, "SELECT * from"
To parse: The ECT is first to be matched, and the next must match the right position of the Select to complete the match.
Here's an example of a combination of order and reverse look: (? <=select). * (? =from)
The result is that the characters between select and from are matched, "select * from", note that the result is included in the space Oh, does not show the red part because the space cannot be labeled red ...
If you do not understand, look back a few more times introduction, will certainly understand.
[Regular expression]pcre surround-look function