Sometimes we need to match nested hierarchical structures like (100*(50 + 15)
In this case, use/(. + /)
It will only match the content between the leftmost left and rightmost right brackets (here we are discussing the greedy pattern, and the lazy pattern also has the following problems ). Assume that the numbers of left and right brackets in the original string are not the same, for example (5/(3 + 2 )))
, Then the number of the two in our matching results will not be equal. Is there a way to match the longest pair of brackets in such a string?
To avoid (
And /(
Confuse your brain completely. Let's replace parentheses with Angle brackets. Now our problem is how to set XX <AA <BBB> AA> YY
In such a string, is the content in the longest pair of angle brackets captured?
The following syntax structure is required:
- (? 'Group ')
Name the captured content as a group and press it into a stack)
- (? '-Group ')
From the stack, the capture content named group is pushed into the stack. If the stack is empty, the matching of this group fails.
- (? (Group) Yes | No)
If the capture content named group exists on the stack, continue to match the expression of part yes; otherwise, continue to match part No.
- (?!)
Assertion with Zero Width and negative direction, attempts to match always fail because there is no suffix expression
Code snippet
(2)
[Code]
Regular Expression description
01 |
<# Left parenthesis of the outermost layer |
02 |
[^ <>] * # The left brackets behind the outermost layer are not the content of the brackets. |
05 |
(?
'Open'
<) # When the left bracket is met, write
"Open" |
06 |
[^ <>] * # Match the content not enclosed by brackets |
09 |
(?
'-Open'
>) # Touch the right parenthesis and erase
"Open" |
10 |
[^ <>] * # Match the content not enclosed by brackets |
13 |
(? (Open )(?!)) # In front of the outermost right parenthesis, check whether there is any non-erased on the blackboard
"Open"
Otherwise, the matching fails. |
15 |
> # Outer right brackets |
[Code]
Application Example -- match the <div> tag
1 |
<div[^>]*>[^<>]*(((?
'Open'
<div[^>]*>)[^<>]*)+((?
'-Open'
</div>)[^<>]*)+)*(?(Open)(?!))</div> |