只删除字符串中的数字



我得到了这样的元素:

<span class="narrowValue">&nbsp;(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)"]}

但我不需要括号。

我怎么能只得到数字呢?

@department_hash["department"] << department.text[/[d,]+/]

使用以下修改:

@department_hash["department"] << department.text.gsub(/[^d,]/,"")

在将文本推送到数组之前,去掉括号。

@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:]]与nbsp匹配,但s与nbsp不匹配。

相关内容

  • 没有找到相关文章

最新更新