Possible Duplicate:
RegEx match open tags except XHTML self-contained tags
I have a string which is html like this
<html>
<div>
<p>this is sample content</p>
</div>
<div>
<p>this is another sample</p>
<span class="test">this sample should not caught</span>
<div>
this is another sample
</div>
</div>
</html>
now i want to search the word sample from this string, here i should not get the “sample” which is inside the <span>...</span>
I want this to be done using regex, i tried a lot but i cant do it, any help is greatful.
Thanks in advance.
This is quite brittle and fails if there can be nested
spantags. If you don’t have those, tryThis matches
sampleonly if the next followingspantag (if any) is not a closing tag.Explanation:
To match
sampleonly if it’s neither within aspannor ap, you can useBut all this depends entirely on tags being unnested (i. e., no two tags of the same kind may be nested) and correctly balanced (which often isn’t given with
ptags).