Group capture, conditional expression, and balanced group of regular expressions

Source: Internet
Author: User

In fact, I would like to say that the balance group, but I would like to write an article for 0 of the basic friends to read, I hope you are a good understanding of the balance group do not know how to read friends. If you already know, look at it anyway. A little more thought.

Group Capture-()

is to use parentheses to expand the content to match.

named group capture-(?<name>) or (?) Name ')

is to increase the?< group name > or? On the basis of group capture. Group name '

To reinforce the impression, for instance,

Axaxbxxxbxxx

Group capture:

(a) X

One match result will get AX, where group 1 captures the result as a

named group capture:

The same text, using (? <data>a) x

One match result, you can get the group match result by the group ordinal number 1, or the group name data

conditional expression-(? <data>.) (? (data) Yes|no)
Very happy, we started to cut to the chase. Conditional expression allows us to have a certain ability of logical judgment. Like what:

Text:

Text [12] and "56" ABCD

Requirements:

Find a number in the middle of [] or ""

We naturally have to consider the pairing of ["or"] these two wrongs, which is just a matter of using a conditional expression

(? <= (? <o1>/[) | (? <o2>/"))/d+ (? = (? O1)/]|/"))

Don't look dizzy, let me slowly tell you this expression of writing ideas.

First of all, what we want is the middle number, what if there are other things to do? Of course is the whole throw away, we do not intend to digress from the complications of the [text] also captured, or have fault tolerance [12] captured in, we are discussing the problem, on the topic and on it.

My first thought should be to use the <=exp/d+ (? =exp2), so that the end result would be only numbers.

So, exp how to write it.

Very simple, (? <=[/[/"]), so that we can't do before and after the bracket type pairing, OK, we take [and" capture separately, and record the corresponding grouping, so that can be referenced later.)

The previous section becomes:

(? <= (? <o1>/[) | (? <o2> "))

In this part of the list, we capture a position, preceded by [or], and if the front is [, then the packet O1 captured, and vice versa O2 caught, so far, not much. But in order to get the corresponding match, we cooperate with the conditional expression, it is much more convenient.

Exp2

We can write it as

(?=(? (O1)/]|/"))

What does that mean?

(? (O1) /]|/")

Indicates that the O1 packet capture is checked here, and if the capture succeeds, the execution/] match, and vice versa, the execution/"match. In this way, we can use conditional expressions to determine the corresponding relationship between [] and "".

The Balance Group (?<group>) (?<-group>) ( Group)?!)

This noun has been used for a long time, there is no reference to the source, "C # string and regular expression" book does not mention, does not matter where the source, but this name, but let a simple concept become complicated, maybe I also stupid, get a long time to understand, in fact, very simple things.

To put it bluntly, it is a high-level use of named groupings, which we write (?<group>) to push the captured content onto the stack, while another advanced use is (?<-group>) to eject the stack of elements that have been pressed onto the stack, (? Group)?!) Is the conditional expression we just saw, and if a group group is captured, execute?! An expression,?! Is that the expression is false, the match fails.

It may be easier to understand a little, for example, without a standard notation.

For example, text:

Xxxxaxxaxxaxxbxxbxxbxxxx

We can do a...b verification in code mode.

[C-sharp] view Plain copy string test = "Xxxxaxxaxxaxxbxxbxxbxxxx"; Match m = regex.match (test, "a" (? <o>a) | (? <-O>B) |   [^ab]+) +b "); if (m.groups["O"].   Captures.count > 0) {Console.WriteLine ("Error, not complete a...b correspondence, with separate a existence"); else {Console.WriteLine ("Very well, a...b corresponds.")   "); }

You can judge in code without using a conditional expression.

What is the point of doing this example? The meaning of the so-called "balance Group" working principle, is to check whether there is no pop-up stack of the packet, if there is, it shows that the pairing is not there, and vice versa. This is the common sense of the balance group.

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.