Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 仅删除字符串中的数字_Ruby_Nokogiri - Fatal编程技术网

Ruby 仅删除字符串中的数字

Ruby 仅删除字符串中的数字,ruby,nokogiri,Ruby,Nokogiri,我有这样的元素: <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 {"depa

我有这样的元素:

<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 = Hash.new {|h,k| h[k]=[]}
department.css('.narrowValue').each do | department |
  @department_hash["department"] << department.text.gsub(/^[() \u00a0]+|[() \u00a0]+$/, '')
end

我怎样才能只得到数字呢

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

@department_hash = Hash.new {|h,k| h[k]=[]}
department.css('.narrowValue').each do | department |
  @department_hash["department"] << department.text.gsub(/^[() \u00a0]+|[() \u00a0]+$/, '')
end

[[:space:]
匹配nbsp,但
\s
不匹配nbsp。

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

@department_hash = Hash.new {|h,k| h[k]=[]}
department.css('.narrowValue').each do | department |
  @department_hash["department"] << department.text.gsub(/^[() \u00a0]+|[() \u00a0]+$/, '')
end

[[:space:]
匹配nbsp,但
\s
不匹配nbsp。

使用以下修改:

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

@department\u hash[“department”]使用以下修改:

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

@department\u hash[“department”]
@department\u hash[“department”]奇怪的是,我试过了,只删除了一个括号(第一个)。@alexchenco,你用了
String#sub
而不是
String#gsub
?没有。奇怪。不知道发生了什么。@alexchenco,我在哪里可以得到html?这是html:奇怪,我试过了,只删除了一个括号(第一个)。@alexchenco,你用了
String\sub
而不是
String\gsub
?没有。奇怪。不确定发生了什么。@alexchenco,我在哪里可以得到html?这是html:是的。你不需要gsub,是的。你不需要gsub。