.NET regular expression in the regular expression: the weapon of handling nested structures
This article discusses the unique balance group characteristics of the .NET regular expression engine, which allows multiple instances of the duplicate group in the regular expression mode of capture and operation.
Repeat group
Unlike other regular expression engines, .NET allowed access to multiple capture results of the same capture group. For example:
<code>(.)+</code>
Copy after login
Apply to the string "ABCD":
- .NET regular expression engine stored all four capture (one each character) in a stack.
- The first group will include these capture:
CaptureCollection
0: "a"
Balance Group
Balance group indicates
, which allows to selectively remove the capture from the stack. When encountering a balance group, it checks whether there is any capture in the stack of the specified group. If so, the last capture (removed) pops up from the stack of the group.
For example, consider the following regular expression: (?<name>)
This regular expression matching is two words separated by non -word characters.
<code>(?<word>\w+)\W+(?<-word>\w+)</code>
Copy after login
The balance group will be removed in the stack created by the first - .
-
(?<-word>)
Condition mode
(?<word>)
Condition mode, written as , combined with the balance group. By using the empty stack stack behavior of the balance group, the condition mode allows to verify more complicated patterns based on whether the stack of the specified balance group is empty.
For example, the following regular expression verification verify whether the brackets of the strings are correctly paired:
(?(condition)truePattern|falsePattern)
It presss each left bracket into the stack and catches up for each right bracket.
Condition mode Make sure that the stack is empty at the end of the string.
<code>^(?:[^()]|(?<Open>[(])|(?<-Open>[)]))*(?(Open)(?!))$</code>
Copy after login
- nested brackets and capture content
- In order to capture the contents of the nested bracket, you can use
(?(Open)(?!$))
grammar. This grammar:
The capture from the stack B;
Press the content of the capture and the current group into the stack A.
(?<a-b>)
Using this function, the following regular expression can not only verify the parentheses string, but also capture the content of each nesting:
-
Balance group, conditional mode and - grammar provide powerful functions for high -level regular expression matching, especially when processing nested structures.
The above is the detailed content of How Do .NET Regular Expressions Use Balancing Groups to Handle Nested Structures?. For more information, please follow other related articles on the PHP Chinese website!