re = /(?(DEFINE)
(?P<table> # To match nested table tags
<table\b[^>]*>
(?:
(?!<\/?table\b[^>]*>).
|
(?&table)
)*
<\/table\s*>
)
(?P<paragraph> # To match nested p tags
<p\b[^>]*>
(?:
(?!<\/?p\b[^>]*>).
|
(?¶graph)
)*
<\/p\s*>
)
)
(?&table)(*SKIP)(*FAIL) # Let's skip table tags
|
(?¶graph) # And match p tags/xi
str = 'test <table><p>wuuut</p></table><p>foo bar</p> test1 <p>baz qux</p> test3'
# 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