[Regular Expression] Pcre Surround function

Source: Internet
Author: User

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

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.