Regular Expression advanced application: how to match nested DIV

Source: Internet
Author: User

Help you! Sorry to attract everyone's attention...

This is now the case:

Code
String testInput = @ "asdfs <ul id = 'aas 'class = 'sdfsdfsadfs'> <li> Hello, everyone! </Li> <li> thank you! </Li> </ul> bbbb ";
Regex reg = new Regex (@"(? <Ul> <ul [\ s \ S] *? \> [\ S \ S] *? </Ul>) ", RegexOptions. Multiline | RegexOptions. IgnoreCase );
MatchCollection ML = reg. Matches (testInput );
Foreach (Match M in ML)
{
If (M. Success)
{
TextBox1.Text + = M. Groups ["ul"]. Value;
}
}


String testInput = @ "asdfs <ul id = 'aas 'class = 'sdfsdfsadfs'> <li> Hello, everyone! </Li> <li> thank you! </Li>
</Ul> bbbb ";
Regex reg = new Regex (@"(? <Ul> <ul [\ s \ S] *? \> [\ S \ S] *? </Ul>) ", RegexOptions. Multiline | RegexOptions. IgnoreCase );
MatchCollection ML = reg. Matches (testInput );
Foreach (Match M in ML)
{
If (M. Success)
{
This. TextBox1.Text + = M. Groups ["ul"]. Value;
}
}


String testInput = @ "<ul id = 'aas 'class = 'cc'>
<Li> Hello everyone </li>
<Li> please! </Li> </ul> ";
Regex reg = new Regex (@"(? <Ul> <ul [\ s \ S] *? \> [\ S \ S] *? </Ul>) ", RegexOptions. Multiline | RegexOptions. IgnoreCase );
MatchCollection ML = reg. Matches (webCode );
Foreach (Match M in ML)
{
If (M. Success)
{
Response. Write (M. Groups ["ul"]. Value + "<br/> ");
}
}

If the string is testInput, the matching is good.

You can get the expected result,

<Ul id = 'aas 'class = 'sdfsdfsadfs'>
<Li> Hello, everyone! </Li>
 
<Li> thank you! </Li>
</Ul>

However, if testInput is changed to the following:

 

Code
@ "Asdfs <ul id = 'aas 'class = 'sdfsdfsadfs'>
<Li> Hello, everyone! </Li>
<Li> <ul id = 'cdd'> <li> experts, I am an interference expert </li> <li>, I am interfering with matching </li> </ul> </li>
<Li> thank you! </Li>
</Ul> bbbb ";

The result I got is

<Ul id = 'aas 'class = 'sdfsdfsadfs'>
<Li> Hello, everyone! </Li>
<Li> <ul id = 'cdd'> <li> experts, I am an interference expert </li> <li>, I am interfering with matching </li> </ul>

The expected result is:

Code
<Ul id = 'aas 'class = 'sdfsdfsadfs'>
<Li> Hello, everyone! </Li>
<Li> <ul id = 'cdd'> <li> experts, I am an interference expert </li> <li>, I am interfering with matching </li> </ul> </li>
<Li> thank you! </Li>
</Ul>

Contains a complete ul nesting,

Ladies and gentlemen, let's take a look at how to write this regular expression. Thank you.

 

 

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.