请帮我处理下一个任务的正则表达式:我在某个表中有一个“成本”列,但那里的值不同:
['1.22','1,22','$1.22','1,22$','$ 1.22']
我需要删除除digits
and,
和之外的每个字符.
。所以我需要得到一个总是可以解析为浮点数的值。
a.map {|i| i.gsub(/[^\d,\.]/, '')}
# => ["1.22", "1,22", "1.22", "1,22", "1.22"]
尝试这个:
yourStr.gsub(/[^0-9,.]/, "")
要提取数字:
a = ["1.22", "1,22", "$1.22", "1,22$", "$ 1.22"]
a.map {|s| s[/[\d.,]+/] }
#=> ["1.22", "1,22", "1.22", "1,22", "1.22"]
假设逗号,
应该被视为小数点.
(如'1,22'
-> 1.22
),这应该将您的值转换为浮点数:
a = ["1.22", "1,22", "$1.22", "1,22$", "$ 1.22"]
a.map {|s| s[/[\d.,]+/].gsub(',','.').to_f }
#=> [1.22, 1.22, 1.22, 1.22, 1.22]
你可以替换所有的空白,全部'$'
由''
另一个:
a= ['1.22','1,22','$1.22','1,22$','$ 1.22']
a.map{|i| i[/\d+.\d+/]}
# => ["1.22", "1,22", "1.22", "1,22", "1.22"]
str = "a1234c324ee4r"
str.delete("^0-9")
它从字符串中删除所有字符并返回所有整数