re = /
<
(?:
!--[^-]*(?:-(?!->)[^-]*)*-->[^<]*(*SKIP)(*F) # skip comments
|
( # group 1
(p|div) # tag name in group 2
[^"'>]*+ #'"# all that is not a quote or a closing angle bracket
(?: # quoted attributes
"[^\\"]*(?:\\.[^\\"]*)*+" [^"'>]* #'"# double quote
|
'[^\\']*(?:\\.[^\\']*)*+' [^"'>]* #'"# single quote
)*+
>
\s*(?: \s*)* # added (bobble bubble)
(?:
<!--[^-]*(?:-(?!->)[^-]*)*+--> \s* # html comments
|
<(?1) \s* # recursion with the group 1
)*+
\s*(?: \s*)* # added (bobble bubble)
<\/\2> # closing tag
) # end of the group 1
)
/xi
str = '<div class="blahblahblah testing"><ul><li>Test 1</li><li>Test 2</li></ul></div><div> </div><p> </p><div class="blahblah"> </div><div class="test2"><p class="blah"> </p><p> </p></div><div class="blahblah"> </div><p> </p><div class="blahblah">Testing</div><div><p> </p></div>
<p> </p>
<p> <p> </p> </p>
<div> <p><p> </p> </p> </div>'
# Print the match result
str.scan(re) do |match|
puts match.to_s
end
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Ruby, please visit: http://ruby-doc.org/core-2.2.0/Regexp.html