0

我有这样的元素:

<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)"]}

但我不需要括号。

我怎样才能只得到数字?

4

3 回答 3

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

于 2013-08-12T05:27:55.687 回答
1

使用以下修改:

@department_hash["department"] << department.text.gsub(/[^\d,]/,"")
于 2013-08-12T05:28:41.820 回答
1
@department_hash["department"] << department.text[/[\d,]+/]
于 2013-08-12T06:53:52.890 回答