-2

how to get innerhtml with nested tags using regex

Example:

<div class="def">
<ul>
<li>..</li>
<li>..</li>
</ul>
</div>

<div class="def">
<ul>
<li>..</li>
<li>..</li>
</ul>
</div>

I need only the first div content of this code "(?s)(<div\\sclass=\"def\">(.+)<\\/div>)" when ever i use this code i get both the div contents

How to get just the first one alone ?

Please help...

Mohammed Aslam
  • 995
  • 9
  • 14

1 Answers1

7

This is becuase of .+ in your regex..Change it to .+?

But you should NEVER use REGEX for parsing html..

Use an HTML PARSER and here's how to get div


Why use parser?

Consider your regex..There are infinite number of cases where you could break your code

  • Your regex won't work if there are nested divs
  • Some divs dont have an ending tag!(except XHTML)

To summarize don't ever ever ever ever ever ever use regex for parsing html

Community
  • 1
  • 1
Anirudha
  • 32,393
  • 7
  • 68
  • 89