Please, help me with a regexp for the next task: I have a 'cost' column in some table, but the values there are different:
['1.22','1,22','$1.22','1,22$','$ 1.22']
I need to remove every character except digits
and ,
and .
. So I need to get a value that always can be parsed as Float.
In Ruby, we can permanently delete characters from a string by using the string. delete method. It returns a new string with the specified characters removed.
Delete - (. Delete is the most familiar Ruby method, and it does exactly what you would think: deletes a sub-string from a string. It will search the whole string and remove all characters that match your substring. The downside of delete is that it can only be used with strings, not RegExs.
=~ is Ruby's pattern-matching operator. It matches a regular expression on the left to a string on the right. If a match is found, the index of first match in string is returned. If the string cannot be found, nil will be returned.
You need to use "\n" not '\n' in your gsub.
a.map {|i| i.gsub(/[^\d,\.]/, '')}
# => ["1.22", "1,22", "1.22", "1,22", "1.22"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With