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?
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)Note that if there is no match then
String#matchreturnsnilso if the values might not exist you would need a check e.g.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.Take a look at the documentation for MatchData, in particular the captures method for more details.