Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6180613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:52:30+00:00 2026-05-24T00:52:30+00:00

When I do puts /<title>(.*?)<\/title>/.match(html) I get <h2>foobar</h2> But I want just foobar What’s

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T00:52:31+00:00Added an answer on May 24, 2026 at 12:52 am

    The most elegant way would be to parse HTML with an HTML parser:

    require 'nokogiri'
    
    html  = '<title><h2>Pancakes</h2></title>'
    doc   = Nokogiri::HTML(html)
    title = doc.at('title').text
    # title is now 'Pancakes'
    

    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:

    <title><strong>Where</strong> is <span>pancakes</span> <em>house?</em></title>
    

    Trying to handle something like that with a single regex is going to be ugly but doc.at('title').text handles 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want do puts blob but if the blob variable doesn't exist, I get
This is my index.html.haml: = stylesheet_link_tag 'user' .title %h1 Port Testing = form_tag('port_testing/test', method:
Probably the most vague title in SO, but it is basically what I am
require 'coderay' puts CodeRay.scan('puts Hello, world!', :ruby).page This code will print full HTML page
I just started with Ruby On Rails, and want to create a simple web
I want to process all links but external ones from the whole web site.
I have a simple but huge xml file like below. I want to parse
I have a string (@description) that contains HTML code and I want to extract
My title doesn't quite sum up what I need correctly but hopefully it will
FIXED I'm sorry for the bad title, but I spend 5 minuits on it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.