1

Hi I've been struggling with this for the last hour and am no closer. How exactly do I strip everything except numbers, commas and decimal points from a rails string? The closest I have so far is:-

rate = rate.gsub!(/[^0-9]/i, '')

This strips everything but the numbers. When I try add commas to the expression, everything is getting stripped. I got the aboves from somewhere else and as far as I can gather:

^ = not

Everything to the left of the comma gets replaced by what's in the '' on the right

No idea what the /i does

I'm very new to gsub. Does anyone know of a good tutorial on building expressions?

Thanks

Al D
  • 657
  • 2
  • 10
  • 27
  • You can also include the comma and decimal point in your square brackets, just escape the decimal point with \ because otherwise it stands for any character. – codess Mar 06 '15 at 21:22
  • The 'i' modifier means ignore case. – codess Mar 06 '15 at 21:22

3 Answers3

3

Try:

rate = rate.gsub(/[^0-9,\.]/, '')

Basically, you know the ^ means not when inside the character class brackets [] which you are using, and then you can just add the comma to the list. The decimal needs to be escaped with a backslash because in regular expressions they are a special character that means "match anything".

Also, be aware of whether you are using gsub or gsub!

gsub! has the bang, so it edits the instance of the string you're passing in, rather than returning another one.

So if using gsub! it would be:

rate.gsub!(/[^0-9,\.]/, '')

And rate would be altered.

If you do not want to alter the original variable, then you can use the version without the bang (and assign it to a different var):

cleaned_rate = rate.gsub!(/[^0-9,\.]/, '')

I'd just google for tutorials. I haven't used one. Regexes are a LOT of time and trial and error (and table-flipping).

This is a cool tool to use with a mini cheat-sheet on it for ruby that allows you to quickly edit and test your expression:

http://rubular.com/

Steph Rose
  • 2,126
  • 3
  • 23
  • 35
  • Thanks a mil, that worked perfectly. I tried adding the comma and decimal point to the [] but I hadn't escaped the decimal point, which was causing an error – Al D Mar 06 '15 at 21:33
  • 1
    One note: `^` indicates negation in a character class (e.g. [^0-9]); otherwise, `^` indicates start of string. – orde Mar 06 '15 at 21:33
  • I don't believe you need to escape the decimal point inside a character class. – Cary Swoveland Mar 06 '15 at 22:02
1

You can just add the comma and period in the square-bracketed expression:

rate.gsub(/[^0-9,.]/, '')

You don't need the i for case-insensitivity for numbers and symbols.

There's lots of info on regular expressions, regex, etc. Maybe search for those instead of gsub.

Greg
  • 76
  • 6
  • Actually, you need a backslash to escape the decimal, which is a special character in regular expressions. – Steph Rose Mar 06 '15 at 21:24
  • Yes Steph is correct. I had tried exactly what you said Greg and it was causing an error – Al D Mar 06 '15 at 21:33
  • I respectfully disagree @StephRose. It wouldn't make sense to use a dot to mean any character in a bracketed expression where you're referencing specific characters. What I typed above works fine in my Rails console. See [here](http://stackoverflow.com/questions/19976018/does-a-dot-have-to-be-escaped-in-a-character-class-square-brackets-of-a-regula). Granted, I can't explain Al D's error. – Greg Mar 07 '15 at 00:18
  • 1
    Your regex is correct: `"cat9.,dog".gsub(/[^0-9,.]/, '') #=> "9.,"`. As I expect you know, the rules for escaping characters within character classes are different from those that apply to characters outside character classes. Periods do not need to be escaped within character classes. Think about it. What would be the point of matching any character (or no character) within a character class? – Cary Swoveland Mar 07 '15 at 00:30
  • Y'know, that's my mistake, I guess that didn't occur to me, although it makes a lot of sense. I wonder if the OP put the comma and decimal outside of the character classes originally and that caused the anomaly. I stand corrected. (And always appreciate disagreements when we learn from them!) – Steph Rose Mar 07 '15 at 04:08
1

You can use this:

rate = rate.gsub!(/[^0-9\.\,]/g,'')

Also check this out to learn more about regular expressions: http://www.regexr.com/

Santiago Suárez
  • 586
  • 7
  • 12
  • Commas never have to be escaped in regexes. Periods do not need to be escaped within character classes. (See @Greg's answer.) – Cary Swoveland Mar 07 '15 at 00:33