Replace content in a file between two markers

Using ruby ​​(not rails), I am trying to figure out how to replace (not add) a specific block in a static file with a string. For example, in static_file.html I want to replace everything between the html comments "start" and "end":

<p>lorem ipsum blah blah ipsum</p> <!--start--> REPLACE MULTI-LINE CONTENT HERE... <!--end--> <p>other stuff still here...</p> 

Some of the answers here are useful for pasting text into a specific place, but not between them.

+4
source share
3 answers

There is a function here to handle it for you. Just pass it the file path and content to replace between these HTML comment blocks:

So far, your comment blocks are always formatted the same way: <- start → and <! - end →, this will work.

 def replace(file_path, contents) file = File.open(file_path, "r+") html = "" while(!file.eof?) html += file.readline end file.close() return html.gsub(/<!--start-->(.*)<!--end-->/im, contents) end 
+4
source

simple answer:

 str = "FOO\n\BAR\nblah \nblah BAZ\nBLOOP" str.gsub(/BAR.*BAZ/m,"SEE") 

I'm not sure if this is enough for what you are trying to do. The key here is the "m" at the end of regexp to indicate a multi-line line. If this is a template for some values, you might want to look at something like ERB templates instead of gsub. Also, be careful what you need to avoid in your regular expressions.

+2
source

This is a simplified example of how to do this 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 searching for the comment, moving on 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 will always be simple, without the ability to have lines that violate your search patterns, you can go with search / replace.

If you check, you will see that for any non-trivial HTML manipulations you have to go with a parser. This is due to the fact that they are dealing with the actual structure of the document, so if the document changes, it is more likely that the parser will not be confused.

+1
source

Source: https://habr.com/ru/post/1337635/


All Articles