正则表达式解析使用Nokogiri

The*_*hts 4 ruby regex nokogiri

使用Nokogiri,我需要解析一个块:

<div class="some_class">
  12 AB / 4+ CD
  <br/>
  2,600 Dollars
  <br/> 
</div>
Run Code Online (Sandbox Code Playgroud)

如果它们存在ab,我需要得到它们cddollars值.

ab = p.css(".some_class").text[....some regex....]
cd = p.css(".some_class").text[....some regex....]
dollars = p.css(".some_class").text[....some regex....]
Run Code Online (Sandbox Code Playgroud)

那是对的吗?如果是这样,有人可以帮我用正则表达式来解析ab,cddollars值?

mik*_*kej 6

为了获得更好的答案,您必须明确说明AB,CD和Dollar值的格式,但这是基于给出的示例的解决方案.它使用正则表达式分组()来捕获我们感兴趣的信息.(有关详细信息,请参阅答案底部)

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"
Run Code Online (Sandbox Code Playgroud)

请注意,如果没有匹配则String#match返回nil,如果值可能不存在,则需要检查,例如

if match = text.match(/([\d,]+) Dollars/)
  dollars = match.captures[0]
end
Run Code Online (Sandbox Code Playgroud)

捕获的其他说明

为了匹配AB的数量,我们需要一种模式/\d+ AB/来识别文本的正确部分.但是,我们真的只对数字部分感兴趣所以我们用括号括起来,以便我们可以提取它.例如

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
Run Code Online (Sandbox Code Playgroud)

查看MatchData的文档,特别是捕获方法以获取更多详细信息.