Remove   from Ruby String -
i try parse data , meet trouble clean symbol. knew "space" realy got trouble clean string code:
require 'rubygems' require 'mechanize' agent = mechanize.new page = agent.get('my_page.hmtl') price = page.search('#product_buy .price').text.to_s.gsub(/\s+/, "").gsub(" ","").gsub(" ", "") puts price and result got "4 162" - dat spaces. don't know do. please meet issue previously. thank you
html escape codes don't mean ruby's regex engine. looking " " literal characters, not thin space. instead, versions of ruby >= 1.8 support unicode in strings, meaning can use unicode code point corresponding thin space make substitution. unicode code point thin space 0x2009, meaning can reference in ruby string \u2009.
additionally, instead of calling some_string.gsub('some_string', ''), can call some_string.delete('some_string').
note isn't appropriate situations, because delete removes instances of characters appearing in intersection of arguments, while gsub remove segments matching pattern provided. example, 'hellohi'.gsub('hello', '') == "hi", while 'hellohi'.delete('hello') == 'i').
in specific case, i'd use like:
price = page.search('#product_buy .price').text.delete('\u2009\s')
Comments
Post a Comment