C # Regular Expression matching

Source: Internet
Author: User

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.
03     
(
04         
(
05             
(?
'Open'
<) # When the left bracket is met, write
"Open"
06             
[^ <>] * # Match the content not enclosed by brackets
07         
)+
08         
(
09             
(?
'-Open'
>) # Touch the right parenthesis and erase
"Open"
10             
[^ <>] * # Match the content not enclosed by brackets
11         
)+
12     
)*
13     
(? (Open )(?!)) # In front of the outermost right parenthesis, check whether there is any non-erased on the blackboard
"Open"
Otherwise, the matching fails.
14  
15 > # Outer right brackets
[Code]
Application Example -- match the <div> tag
1 <div[^>]*>[^<>]*(((?
'Open'
<div[^>]*>)[^<>]*)+((?
'-Open'
</div>)[^<>]*)+)*(?(Open)(?!))</div>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.