This is a simplified example of how to do it using a parser:
require 'nokogiri'
html = '<p>lorem ipsum blah blah ipsum</p>
<!--start-->
REPLACE MULTI-LINE
CONTENT HERE...
<!--end-->
<p>other stuff still here...</p>'
doc = Nokogiri.HTML(html)
puts doc.to_html
After parsing we get:
# >> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
# >> <html><body>
# >> <p>lorem ipsum blah blah ipsum</p>
# >>
# >> <!--start-->
# >> REPLACE MULTI-LINE
# >> CONTENT HERE...
# >> <!--end-->
# >>
# >> <p>other stuff still here...</p>
# >> </body></html>
doc.at('//comment()/following-sibling::text()').content = "\nhello world!\n"
puts doc.to_html
After finding the comment, stepping to the next text()
node and replacing it:
# >> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
# >> <html><body>
# >> <p>lorem ipsum blah blah ipsum</p>
# >>
# >> <!--start-->
# >> hello world!
# >> <!--end-->
# >>
# >> <p>other stuff still here...</p>
# >> </body></html>
If your HTML is always going to be simple, with no possibility of having strings that break your search patterns, then you can go with search/replace.
If you check around, you see that for any non-trivial HTML manipulation you should go with a parser. That's because they deal with the actual structure of the document, so if the document changes, there's a better chance of the parser not being confused.