Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby remove everything except some characters?

Tags:

regex

ruby

gsub

How can I remove from a string all characters except white spaces, numbers, and some others? Something like this:

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

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

Also, is there a good document about the use of regex in Ruby, like a list of special characters with examples?

like image 868
zajca Avatar asked Dec 05 '22 17:12

zajca


2 Answers

The regex you have is already working correctly. However, you do need to assign the result back to the string you're operating on. Otherwise, you're not changing the string (.gsub() does not modify the string in-place).

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

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

A good resource with special consideration of Ruby regexes is the Regular Expressions Cookbook by Jan Goyvaerts and Steven Levithan. A good online tutorial by the same author is here.

like image 157
Tim Pietzcker Avatar answered Dec 07 '22 07:12

Tim Pietzcker


Good old String#delete does this without a regular expression. The ^ means 'NOT'.

str = "12eldabc8urp pp"
p str.delete('^0-9ldur<> ') #=> "12ld8ur "
like image 43
steenslag Avatar answered Dec 07 '22 07:12

steenslag