I wrote one beforeArticleExplains how to use regular expressions to perfectly solve the problem of matching HTML nested tags (using regular expressions to match Nested HTML tags), but uses advanced features such as a balance group, it seems that only DOTNET and Perl Regular Expression engines are supported, so the versatility is not high. Some friends leave a message saying that an error will be reported if Java is used directly. I checked later and found that the Java Regular Expression Engine supports fewer features. In version 1.6, you cannot use a naming group (it seems that the Group is supported in version 1.7). Otherwise, the following error will be reported, not to mention the balance group. Therefore, it is unrealistic to implement an infinitely nested match.
Java. util. RegEx. patternsyntaxexception: Look-behind group does not have an obvious maximum length near IndexXx
I searched the internet for a long time and did not find the perfect solution. However, we can implement limited-level HTML Nested Tag matching. The concept is much simpler than the infinite level, and there is no need for so many advanced features.
Example:
<Div id = 'Container'>
<Div style = 'background-color: Gray; 'id = 'footer '>
<A id = 'gotop 'href =' # 'onclick = 'mgjs. gotop (); Return false; '> top </a>
<A id = 'powered' href = 'HTTP: // wordpress.org/'> WordPress </a>
<Div id = 'copyright'>
Copyright & copy; 2009 simple life-Kevin Yang's blog
</Div>
<Div id = 'themeinfo'>
Theme by <a href = 'HTTP: // www.neoease.com/'> mg12 </a>. Valid <a href = 'HTTP: // validator.w3.org/check? Uri = Referer '> XHTML 1.1 </a>
And <a href = 'HTTP: // jigsaw.w3.org/css-validator/'> CSS 3 </a>.
</Div>
</Div>
</Div>
In the above example, we intend to match the nested Div with the ID of footer, andSuppose we know in advance that the footer Div can only nest a level Div.. Let's talk about more levels later.
The match between the start and end labels of footer is simple:
<Div [^>] * id = 'footer '[^>] *>... (The ellipsis here will be filled in later) </div>
The content between the start and end tags is in either of the following situations:
- Content A: div tag, and no nested DIV in this Div
- Content B: Any other content
Then there is the constant repetition of the two types of content. The regular expression is as follows:
(<Div [^>] *> .*? </Div>|.)*?
Note that the question mark must be added at the end. Otherwise, the closed tag of footer may fail to match because of the greedy matching feature of regular expressions.
OK,Matching a maximum of nested level DivThe regular expression is as follows:
<Div [^>] * id = 'footer '[^>] *> (<Div [^>] *> .*? </Div>| .)*? </Div>
So what if the footer tag contains a maximum of two levels of Div nesting?
In fact, it is not difficult. We only need to replace the dot in content. Modify as follows:
<Div [^>] * id = 'footer '[^>] *> (<Div [^>] *>(<Div [^>] *> .*? </Div> | .)*? </Div>| .)*? </Div>
Here you may know how to write a regular expression if you want to match up to three-level Div nesting:
<Div [^>] * id = 'footer '[^>] *> (<Div [^>] *>(<Div [^>] *>(<Div [^>] *> .*? </Div> | .)*? </Div> | .)*? </Div>| .)*? </Div>
So in fact, as long as your HTML structure is not very complex, that is to say, nesting is not very deep, you can use this method to match Nested HTML tags.
This regular expression can be used in both Java and JavaScript because it does not have any advanced features.
--Kevin Yang