How to replace a group of reserved characters with "" in Ruby on Rails?

I want to replace the following reserved characters with spaces:

+ - & | ! ( ) { } [ ] ^ " ~ * ? : \ 

This is my code, but it does not work. Did I miss something?

 keyword = keyword.gsub(/\\+-&\\|!\\(\\)\\{\\}\\[\\]\\^"~\\*\\?:\\\\/, ' ') 
+4
source share
4 answers

This is what tr for:

 keyword.tr '-+&|!(){}[]^"~*?:\\', " " #=> " " 
+10
source

Here is an example showing the speed difference between gsub and tr :

 require 'benchmark' require 'pp' STR = '+ - & | ! ( ) { } [ ] ^ " ~ * ? : \\' LONG_STR = STR * 1_000 N = 1_000 puts `ruby -v` pp STR.gsub(/[+&|!(){}\[\]^"~*:?\\-]/, ' ') pp STR.tr('-+&|!(){}[]^"~*?:\\', ' ') Benchmark.bm(5) do |b| b.report('gsub') { N.times { LONG_STR.gsub(/[+&|!(){}\[\]^"~*:?\\-]/, ' ') } } b.report('tr') { N.times { LONG_STR.tr('+&|!(){}[]^"~*:?\\-', ' ') } } end 

And the conclusion:

 ruby 1.8.7 (2012-02-08 patchlevel 358) [universal-darwin12.0] " " " " user system total real gsub 13.300000 0.190000 13.490000 ( 13.524779) tr 0.080000 0.010000 0.090000 ( 0.090045) ruby 1.9.3p392 (2013-02-22 revision 39386) [x86_64-darwin12.2.0] " " " " user system total real gsub 17.890000 0.040000 17.930000 ( 18.016657) tr 0.270000 0.000000 0.270000 ( 0.283021) ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-darwin12.2.0] " " " " user system total real gsub 7.310000 0.020000 7.330000 ( 7.361403) tr 0.140000 0.010000 0.150000 ( 0.145816) 

Interestingly, 1.8.7 failed 1.9.3. I suspect this due to the addition of multibyte character support in 1.9+.

I did some tests with 2.0 and was very pleased with the speed improvement I saw.

+10
source

Just do it.

 keyword.gsub!(/[+\-&|!(){}\[\]^"~*?:\\]/, " ") 

Check:

 >> keyword = '+ - & | ! ( ) { } [ ] ^ " ~ * ? : \\' => "+ - & | ! ( ) { } [ ] ^ \" ~ * ? : \\" >> keyword.gsub!(/[+\-&|!(){}\[\]^"~*?:\\]/, " ") => " " 

Character classes (in the appendix [] ) are easier to reason in this case. You need to exit - and [ and ] and \ .

+3
source

\ W = any character without a word

 >> keyword = '+ - & | ! ( ) { } [ ] ^ " ~ * ? : \\' => "+ - & | ! ( ) { } [ ] ^ \" ~ * ? : \\" >> keyword.gsub!(/\W/," ") => " " 
0
source

All Articles