Using regular expressions for anything beyond the most trivial and controlled parsing leads to madness. Use Nokogiri, an excellent Ruby XML/HTML parser. For instance:
require 'nokogiri'
xml = <<EOT
<xml>
<foo>foo</foo>
<bar>bar</bar>
</xml>
EOT
doc = Nokogiri::XML(xml)
doc.at('foo').content = 'bar'
doc.at('bar')['class'] = 'cyan'
puts doc.to_xml
Which outputs:
<?xml version="1.0"?>
<xml>
<foo>bar</foo>
<bar class="cyan">bar</bar>
</xml>
Notice the content inside the <foo>
tag changed, along with <bar>
gaining an attribute.
What's important about using a parser is that the content can change, tag parameters can change, their order can move around inside the tag, tags can be split across multiple lines, and a parser will not care, whereas a regular expression will spout flames and stop working.