Recursive matching of Regular Expressions

Source: Internet
Author: User

There is often such a requirement that the content in the parentheses be matched,

In general,? The r syntax does not seem to be supported in C #. With some effort, I finally find the following description:

 

/(It should be \ (not escape with/, but escape \)

Match nested Structures
Microsoft has included an interesting innovation to match stable structures (historically, This is what regular expressions cannot do ). This is not easy to grasp-although this section is short, note that it is very obscure.
It may be easier to start with an example, so I use this code as the start:
RegEx r = new RegEx (@ "/(?> [^ ()] + | /((? <Depth>) | /)(? <-Depth> ))*(? (Depth )(?!)) /)");
This matches the First Fully paired bracket group, for example, "Before (Nope (Yes (here) Okay) after" (Yes (here) Okay )". Note that the first left brace is not matched because there is no right brace that matches it.
The following is an overview of how it works:
1. When each "(" is matched ,"(? <Depth>) "add a value here to tell the regular expression system the depth of the nested parentheses ("/"at the beginning of the regular expression is not included here ).
2. When ")" is matched ,"(? <-Depth>) "minus one from the depth value.
3 ,"(? (Depth )(?!)) "Ensure that the depth is zero before matching the last right parenthesis.
The reason it works is that the engine's back-to-back stack stores the track of the matched group. "(? <Depth>) "is just a group structure with a name. It will always match successfully (not match anything ). Since it is placed immediately after "/(", its successful matching (still on the stack until it is removed) is used for the Count of left parentheses.
Another way of writing is "(? <Depth>/() ", I personally prefer this form, instead "/((? <Depth> )". "/)(? <-Depth>) "is the same.
In this way, the count of the matched group named "depth" is created on the back-to-back stack. When we find the right parenthesis, we also want to subtract one from the depth value, which is constructed by the. NET special Syntax "(? <-Depth>) ", which removes the recently matched" depth "group from the stack. If no records exist on the stack ,"(? <-Depth>) "group matching fails, which prevents the Regular Expression System from matching redundant right brackets.
Finally ,"(? (Depth )(?!)) "Is used "(?!) If the "depth" group is still successful so far. If the matching is successful, an unpaired left brace is not "(? <-Depth>) "removed. In this case, we want to stop matching (we do not want to match an unpaired bracket), so we use "(?!) ", It is a" zero-width negative prediction first asserted ", only when the child expression does not match on the right of this position to continue matching.
This is the method for matching nested structures in. Net's regular expression implementation.

 

The above content seems difficult to understand. In fact, if it is easy to understand, you should not understand it. If you can use it, you will be OK and replace () with the character you want, I believe it can solve many of your problems,

 

The following is a test case based on this usage:

  1. Private void button3_click (Object sender, eventargs E)
  2. {
  3. RegEx r = new RegEx (@ "/[(?> [^/[/] + | /[(? <Depth>) |/] (? <-Depth> ))*(? (Depth )(?!)) /] ");
  4. Stringbuilder sb = new stringbuilder ();
  5. Matchstring ("[111 [222 [333] [222 [333] [333]", R, Sb );
  6. MessageBox. Show (sb. tostring (), "obtained information ");
  7. }
  8. Private void matchstring (string outstring, RegEx R, stringbuilder SB)
  9. {
  10. Matchcollection MS = R. Matches (outstring); // get all matches
  11. Foreach (Match m in MS)
  12. {
  13. If (M. Success)
  14. {
  15. SB. appendline (M. Groups [0]. value );
  16. Matchstring (M. groups [0]. value. substring (1, M. groups [0]. value. length-1), R, Sb); // remove the "[" and "]" matched headers and tails to avoid endless loops and recursion, leading to overflow.
  17. }
  18. }
  19. Return;
  20. }

You can get

[111 [222 [333] [222 [333] [333] [222] [333] [333]

Recursive matching of Regular Expressions

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.