Ruby removes everything but some characters?

How to remove all characters from a string except spaces, numbers, and some others? Something like that:



I just need to: 0-9 l d u r < > <space>

Also, is there a good document on using regex in Ruby, like a list of special characters with examples?


source to share

3 answers

The regex is already working correctly. However, you need to revert the result to the line you are working on. Otherwise, you are not changing the line ( .gsub()

not changing the line in place).

You can improve the regex a bit by adding the '+' quantum (so that consecutive characters can be replaced in one go). Also, you don't need to hide the angle brackets:

oneLine = oneLine.gsub(/[^ULDR0-9<>\s]+/i, '')


A good resource with a special focus on Ruby regular expressions is the Regular Expressions Cookbook by Jan Goywaerts and Stephen Levitan. A good online tutorial by the same author is here .



The good old school String#delete

does it without regex. ^

means "NOT".

str = "12eldabc8urp pp"
p str.delete('^0-9ldur<> ') #=> "12ld8ur "




Just for completeness: you don't need a regex for this particular task, it can be done with simple string manipulation :

irb(main):005:0> "asdasd123".tr('^ULDRuldr0-9<>\t\r\n ', '')
=> "dd123"


There is also a method tr!

if you want to replace the old value:

irb(main):009:0> oneLine = 'UasdL asd 123'
irb(main):010:0>!('^ULDRuldr0-9<>\t\r\n ', '')
irb(main):011:0> oneLine
=> "UdL d 123"


It should also be slightly faster (but performance shouldn't be a big issue in Ruby :)



All Articles