When we see a balanced group, recursive matching is too many terminologies. In fact, it is easy to say how to match character pairs that match each other and are nested with each other.
Matching parentheses such. if a function is long and long when you write code, the nested {} will make you dizzy. I don't know which one should be used. how do people know the compiler during compilation. it is identified by a theory similar to a balancing group. there will also be a bunch of angle brackets like <> in HTML and XML. If you don't know how to match the matching brackets, it will be messy. A theory similar to the balancing group is also used here.
First, let's talk about C # stack knowledge.
So how can we make a correct match. in many university courses, the course of data structure may have such an exercise: There is a string containing numbers and Arithmetic Operators and parentheses. how to parse it as an arithmetic expression and get the result. for example, string STR = "(3 + 4) * (9-5)-2"; how to parse it and calculate the result. the simplest way to achieve this is to use the stack data structure. it is a structure of post-import and post-export. adding an element to a stack is called an inbound stack,
Remove an element from the stack and call the stack. if the character '(' then goes to the stack and the character ')' is passed in, not only does not go into the stack, but also deletes the previous. the C # code can be written in this way.
Char input = '(';
Stack <char> stack = new stack <char> ();
If (input = '(')
Stack. Push (input );
If (input = ')')
Stack. Pop ();
If (stack. Count = 0) // determines whether the elements in the stack are empty.
Return true;
Else
Return false;
You may wonder why this is irrelevant to regular expressions. in fact, it is not unrelated, but very relevant. you can also use the stack mentioned above to write code to implement the functions that can be achieved by using a balanced group and recursive matching. it's a little more complicated. for example, we need to add loop judgment. the stack must also be used in the regular expression, but it is encapsulated and you cannot see it.
Balancing group, recursive matching
Stack is used in regular expressions.
(? 'Group' exp)This is actually the name of a group mentioned above. Exp is a string, which is enclosed in parentheses as a group. Group is the group name. except for naming, the backend operation is to import exp into the stack.
Assume that exp is left parentheses (? 'Group' \ (), the operation is actually the same as the previous code if (input = '(') stack. Push (input );
(? '-Group' exp)Here there is an extra-indicating the stack. If exp is a right brace, that is (? '-Group' \), its operation is the same as the previous code if (input =') stack. Pop.Output exp from Stack
(? (Group) Yes | No)If the capture content named group exists on the stack, continue to match the expression of the yes part; otherwise, continue to match the no part. In fact, here yes and no can last year
If yes is removed, no is left and no operations are basically not performed. Only a sign indicating successful or failed matching is returned (?!) To indicate that the match fails. We have mentioned the assertion with Zero Width before. It is a special assertion.
So most of the time we use this
(? (Group )(?!) It means the same as the above Code. If (stack. Count = 0) // judge that the elements in the stack are empty.
Return true;
Else
Return false;
Here is a simple example. If there is a string, match the content in the <>
String source = "** <A <B <Arwen> C> Other> **";
String Pattern = @ "[^ <>] * # match any non-angle brackets
(? 'Group' <) # match <and
[^ <>] * # Match any non-angle brackets
(? '-Group'>) # match> and exit the stack
[^ <>] * # Match any non-angle brackets
(? (Group )(?!)) # Check whether the stack is empty. If it is empty, the matching is successful. Otherwise, the matching fails.
";
Console. writeline (RegEx. Match (source, pattern, regexoptions. ignorepatternwhitespace). value );
The result is
B <Arwen> C