Ruby 仅删除字符串中的数字
我有这样的元素:Ruby 仅删除字符串中的数字,ruby,nokogiri,Ruby,Nokogiri,我有这样的元素: <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 {"depa
<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)"]}
但我不需要括号
@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。