search and replace with ruby regex

Posted by randombits on Stack Overflow See other posts from Stack Overflow or by randombits
Published on 2011-01-16T01:46:12Z Indexed on 2011/01/16 1:53 UTC
Read the original article Hit count: 527

Filed under:
|

I have a text blob field in a MySQL column that contains HTML. I have to change some of the markup, so I figured I'll do it in a ruby script. Ruby is irrelevant here, but it would be nice to see an answer with it. The markup looks like the following:

<h5>foo</h5>
  <table>
    <tbody>
    </tbody>
  </table>

<h5>bar</h5>
  <table>
    <tbody>
    </tbody>
  </table>

<h5>meow</h5>
  <table>
    <tbody>
    </tbody>
  </table>

I need to change just the first <h5>foo</h5> block of each text to <h2>something_else</h2> while leaving the rest of the string alone.

Can't seem to get the proper PCRE regex, using Ruby.

© Stack Overflow or respective owner

Related posts about ruby

Related posts about regex