From: Jeremy Woertink Date: 2009-04-19T13:59:03+09:00 Subject: String replacing help I'm working with Mechanize doing some screen scraping. Because of the project, I have to use an older version of Mechanize for now, I'm using 0.8.4. The goal of what I'm trying to do is take a string and insert pipes '|' before words that are *not* inside of . I have: >> template_body.class => Hpricot::Elements >> template_body.to_html => "
HEAD \n  TITLE \"test\"\nBODY \n
DIV id \"main-div\"\n
CSS-WITH-LINK destination TO :index\n
IMAGE source RENDER image @image\n
max-height m-h\n
" The words I'm trying to insert the pipe before have a non-breaking space tags around them. I had it working where I can iterate through everything and return a new string, but I end up losing all line breaks and non-breaking spaces using new_body = '' template_body.to_html.split(" ").each do |el| el.split("\n").each do |e| unless e.empty? or e =~ /<\/?[^>]*>/ e = '|' + e end end new_body += el end Any ideas? Thanks, ~Jeremy -- Posted via http://www.ruby-forum.com/.