The ultimate ability of regular expressions - Recursive posted on thursday, January 27, 2005 6:26 PM href = "http://blog.sunmast.com/sunmast/services/pingback.aspx" Rel = "pingback" />
Today, when QQ Qiuzhi wrote a matching regular expression, I didn't expect that the guy replied that "recursive elimination", let me see the book of compilation. (NND, he must think of the implementation of the regular expression ...)
It is not discovered that the syntax of the regular expression is not discovered or recursive or can be used indirectly, but today I found an e-book in the hard disk (one chapter), actually there is this. Hirder, record it.
The example is:
/ (?> [^ ()] | / ((?
) | /) (? <- defth>)) * (? (Depth) (?!)) /)
This is a syntax that matches the most effective parentheses, such as:
Before (Nope (Yes (Here) OkayAfter
Matching: (Yes (Here) Okay
Simple translation, this document:
Match nested construction
Microsoft has included an interesting innovation to match stable structures (history, this is what is not done). This is not easy to master - although this section is short, but pay attention to it is very embarrassing.
From an example, it may be more simple, so I start using this code:
Regex r = new regex (@ "/ ((?> (?
This can match the first fully paired parentheses, such as "Before (YES (Here)" (YES OKAY). Note that the first left bracket is not matched because There is no right bracket that it matches.
Below is an overview of how it works:
1, in each "(" When it is matched, "(?
2. When each ")" is matched, "(? <- defth>)" minus one from the depth value.
3, "(? (? (? (?!))" Ensure that the depth is zero before the last right bracket is matched.
It works because the engine's back-reverse stack saves the trajectory of the successful group. "(?
Translation: Is there a way to write "(?
Thus, the count of the group that successfully named "Depth" is established on the backfall stack. When we find the right bracket, we also want to decrease from the depth value. This is implemented by the .NET special syntax "(? <" "" (? <"," DEPTH " Group. If there is no record on the stack, "(? <- defth>) packet match failed, thereby preventing the regular expression system from matching the extra right brackets. Finally, "(? (? (? (? (? (?!)" Is it a "(?!)" Assertion, if the "depth" group is still successful so far. If we are still successful when we match here, there is no pairing left parentheses yet "(? <- defth>)" removal. In this case, we want to stop matching (we don't want to match a unpaired parentheses), so we use "(?!)", It is a "zero width negative prediction first line assertion", only the child expression is not here You can continue to match when the right side of the location is matched.
This is a method of matching the nested structure in the regular expression of .NET.