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 6474505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:35:41+00:00 2026-05-25T06:35:41+00:00

I have a bunch of strings that look, for example, like this: <option value=Spain>Spain</option>

  • 0

I have a bunch of strings that look, for example, like this:

<option value="Spain">Spain</option>

And I want to extract the name of the country from inside.

The easiest way I could think of to do this in Ruby was to use a regular expression of this form:

country  = line.match(/>(.+)</)

However, this returns >Spain<. So I did this:

line.match(/>(.+)</).to_s.gsub!(/<|>/,"")

Works well enough, but I’d be surprised if there’s not a more elegant way to do this? It seems like using a regular expression to declare how to find the thing you want, without actually wanting the enclosing strings that were used to match it to be part of the data that gets returned.

Is there a conventional approach to this problem?

  • 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-25T06:35:41+00:00Added an answer on May 25, 2026 at 6:35 am

    The right way to deal with that string is to use an HTML parser, for example:

    country = Nokogiri::HTML('<option value="Spain">Spain</option>').at('option').text
    

    And if you have several such strings, paste them together and use search:

    html      = '<option value="Spain">Spain</option><option value="Canada">Canada</option>'
    countries = Nokogiri::HTML(html).search('option').map(&:text)
    # ["Spain", "Canada"]
    

    But if you must use a regex, then:

    country = '<option value="Spain">Spain</option>'.match('>([^<]+)<')[1]
    

    Keep in mind that match actually returns a MatchData object and MatchData#to_s:

    Returns the entire matched string.

    But you can access the captured groups using MatchData#[]. And if you don’t like counting, you could use a named capture group as well:

    country = '<option value="Spain">Spain</option>'.match('>(?<name>[^<]+)<')['name']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bunch of strings in perl that all look like this: 10
I have a bunch of strings in php that all look like this: 10
I have a class with a bunch of properties that look like this: public
I have to create a bunch of methods that look like this. The things
I have a bunch of strings like this: 'ABCD99991000' 'XYZ79991000' 'E2493991039' What I really
I have a project with a bunch of classes with methods that look like
I have a bunch of legacy documents that are HTML-like. As in, they look
I have a bunch of strings that are dependent on static dictionaries and each
I have a bunch of string with special escape codes that I want to
I have bunch of strings, some of which are fairly long, like so: movie.titles

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.