Nokogiri is excellent for this:
require 'nokogiri'
doc = Nokogiri::HTML.parse(<<EOT)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<meta name="description" content="Free Web tutorials">
</head>
<body></body>
</html>
EOT
meta = doc.at('meta[@name]')
meta['content'] = 'foo'
puts doc.to_html
# >> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
# >> <html>
# >> <head>
# >> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
# >> <meta name="description" content="foo">
# >> </head>
# >> <body></body>
# >> </html>
If you want to append something to the description's content
:
meta['content'] = meta['content'] + ' by foobar'
Which results in:
# >> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
# >> <html>
# >> <head>
# >> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
# >> <meta name="description" content="Free Web tutorials by foobar">
# >> </head>
# >> <body></body>
# >> </html>
HTML that you don't control can change in wild and wonderful ways if the creators change to different HTML generators. That can break your application unless you use something robust, and regular expressions for HTML are not robust enough.
It's easy to write a pattern to match
<meta name="description" content="Free Web tutorials">
It's not so easy to write one that matches that one day, and then
<meta
name="description"
content="Free Web tutorials"
>
the next.
It's easy to imagine seeing various HTML output styles because the site's content people used different tools, along with some automation. A parser can handle it nicely.