我有这样的元素:
<span class="narrowValue"> (15,728)</span>
我正在这样抓:
@department_hash = Hash.new {|h,k| h[k]=[]}
department.css('.narrowValue').each do | department |
@department_hash["department"] << department.text
end
得到这样的结果:
{"department"=>[" (15,725)", " (243,256)", " (510,337)", " (46,002)", " (14,109)", " (358)", " (5,787)", " (19,818)"]}
但我不需要括号。
我怎样才能获得数字?
答案 0 :(得分:1)
在将文本推送到数组之前,请删除括号。
@department_hash = Hash.new {|h,k| h[k]=[]}
department.css('.narrowValue').each do | department |
@department_hash["department"] << department.text.gsub(/^[() \u00a0]+|[() \u00a0]+$/, '')
end
或者,您可以使用以下正则表达式:
/^[()[:space:]]+|[()[:space:]]+$/
[[:space:]]
匹配,但\s
与nbsp不匹配。
答案 1 :(得分:1)
使用以下修改:
@department_hash["department"] << department.text.gsub(/[^\d,]/,"")
答案 2 :(得分:1)
@department_hash["department"] << department.text[/[\d,]+/]