Title: General construction method of recursive regular expressions
xhd2015
Original posts: http://tieba.baidu.com/p/4117059926
=======================
Let's talk about it, it's corny, how to match nested <>
If you can match
Class regex<>
Congratulations, the first stage of the meeting, look down.
So, with this.
class regex< class< > >
If you only match to the red character, unfortunately, you're wrong
General expressions have no effect on nested pairs of symbols
Not all regular tools support recursion, and the key to implementing recursion is the ability to reference "rules".
Yes, a reference to a rule (not a reverse reference to a capturing group such as \1)
If a rule allows nesting, it means that recursion is allowed. Let's look at what a rule reference is, and the difference between a reverse reference to a capturing group:
The expression (\s|\w) means either \s, or \w, after the parentheses, there is a group number, assuming the number is 1, then
The rule reference is (? 1)
The reverse reference is \1 ← without parentheses
The difference is that the rule reference means that the expression is applied completely here, so (\s|\w) (? 1) is equivalent to (\s|\w) (\s|\w)
The reverse reference to the capturing group is simply to copy the matched characters completely here
Now get to the point where rules refer to how to construct recursive regularization?
What do you mean by saying that the rule reference is the key to recursion?
Recursion is self-contained, so if the rule itself contains itself, then this is recursion. Many nested strings are actually recursive.
For nested <>, the general rule can only match a finite depth of <>, and here are a few things to consider
Match 1 Depth <[^<>]*>
Matching depth 2 <[^<>]*<[^<>]*>[^<>]*>
Match Depth 3 ...
If you write your own 1-3 depth, you'll find the rules are simple.
We found that 2 was generated by 1 recursion, so we tried to remove 1 from 2, using ___ instead, to get
<[^<>]*___[^<>]*>
Here, the particulars can represent generality, so the recursive type is
rn= <[^<>]*Rn-1[^<>]*>
So, the recursive expression is
(<[^<>]* (? 1) *[^<>]*>)
Tested, completely correct
General method: (Side dishes will)
1. Write a nested expression of depth 1
2. Write a nested expression of depth 2
3. Replace the position of 2 in 1 with (? n), which is the entire recursive expression. (n is the number of the group in which it is located)
This method is algorithmic, can be implemented by software, that is, to give a symbol pair, you can generate recursive regular
=======================
Reprint note, in this (I modified) example can be written as follows:
[^<>]+< (? 0) *>
Where (? 0) represents a recursive reference to the entire expression
General construction method of "reprint" recursive regular expression