When I do
puts /<title>(.*?)<\/title>/.match(html)
I get
<h2>foobar</h2>
But I want just
foobar
What’s the most elegant method for doing so?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The most elegant way would be to parse HTML with an HTML parser:
If you try to do this with a regular expression, you will probably fail. For example, if you have an
<h2>in your<title>what’s to prevent you from having something like this:Trying to handle something like that with a single regex is going to be ugly but
doc.at('title').texthandles that as easily as it handles<title>Pancakes</title>or<title><h2>Pancakes</h2></title>.Regular expressions are great tools but they shouldn’t be the only tool in your toolbox.