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

  • Home
  • SEARCH
  • 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 940743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:00:18+00:00 2026-05-15T22:00:18+00:00

Using Nokogiri, I need to parse a block given: <div class=some_class> 12 AB /

  • 0

Using Nokogiri, I need to parse a block given:

<div class="some_class">
  12 AB / 4+ CD
  <br/>
  2,600 Dollars
  <br/> 
</div>

I need to get the ab, cd and dollars values if they exist.

ab = p.css(".some_class").text[....some regex....]
cd = p.css(".some_class").text[....some regex....]
dollars = p.css(".some_class").text[....some regex....]

Is that correct? If so, can someone help me with a regex to parse the ab, cd and dollars values?

  • 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-15T22:00:19+00:00Added an answer on May 15, 2026 at 10:00 pm

    To get a better answer you would have to clarify exactly what format the AB, CD and Dollar values take but here is a solution based on the example given. It uses a regexp grouping () to capture the information we’re interested in. (see the bottom of the answer for more details)

    text = p.css(".some_class").text
    
    # one or more digits followed by a space followed by AB, capture the digits
    ab = text.match(/(\d+) AB/).captures[0] # => "12"
    
    # one of more non digits followed by a literal + followed by CD
    cd = text.match(/(\d+\+) CD/).captures[0] # => "4+"
    
    # digits or commas followed by "Dollars"
    dollars = text.match(/([\d,]+) Dollars/).captures[0] # => "2,600"
    

    Note that if there is no match then String#match returns nil so if the values might not exist you would need a check e.g.

    if match = text.match(/([\d,]+) Dollars/)
      dollars = match.captures[0]
    end
    

    Additional explanation of captures

    To match the amount of AB we need a pattern /\d+ AB/ to identify the right part of the text. However, we’re really only interested in the numeric part so we surround that with brackets so that we can extract it. e.g.

    irb(main):027:0> match = text.match(/(\d+) AB/)
    => #<MatchData:0x2ca3440>           # the match method returns MatchData if there is a match, nil if not
    irb(main):028:0> match.to_s         # match.to_s gives us the entire text that matched the pattern
    => "12 AB"
    irb(main):029:0> match.captures     
    => ["12"]
    # match.captures gives us an array of the parts of the pattern that were enclosed in ()
    # in our example there is just 1 but there could be multiple
    irb(main):030:0> match.captures[0]
    => "12"                             # the first capture - the bit we want
    

    Take a look at the documentation for MatchData, in particular the captures method for more details.

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

Sidebar

Related Questions

I am using mechanize/nokogiri and need to parse out the following HTML string. can
Using Nokogiri and Ruby. I have a page to parse with div id's like:
If I want to parse a HTML block using Nokogiri in Ruby like this:
I am using mechanize/nokogiri and need to parse out a HTML with a lot
I need to parse HTML using Rails and Nokogiri. Here is the HTML: <body>
I am using Nokogiri to analyze some HTML, but, I don't know how get
I'm using Nokogiri to parse an XML response from last.fm. I am currently returning
I am trying to parse XML files using Nokogiri, Ruby and XPath. I usually
I'm using Nokogiri::XML to parse responses from Amazon SimpleDB. The response is something like:
I'm using Nokogiri for a simple example to get council tax bands for a

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.