1
# #!/usr/local/bin/ruby

puts "why doesn't this work??"
pi = ''
special = "[;\`'<>-]"
regex = /[#{special.gsub(/./){|char| "\\#{char}"}}]/

pi = ARGV[0].to_s #takes in console argument to test

if pi == '3.1415926535897932385'
  puts "got it"
end
if pi =~ regex
  puts "stop word"
else 
  puts "incorrect"
end

All I'm trying to do is test whether or not the pi variable contains any of the stop characters, if true, print "stop word" otherwise got it or incorrect respectively. I've tried doing this about ten ways. with scans, include? lines and I feel like this is the best route.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Jonny Moon
  • 21
  • 2
  • Does it fail when you type the special characters in the regex yourself or is it an issue with `gsub`? – Andrew Oct 30 '15 at 02:19
  • backslash is a special character, may need two: "\\" I don't remember if it is ruby or not but I think perl required four if the string was then going to be put in a regex or something – DGM Oct 30 '15 at 02:34
  • There's no example in your question of how it fails. Please give us a minimal example of input that demonstrates the problem, and what you want the code to output for that input. – the Tin Man Oct 30 '15 at 03:45

2 Answers2

1

I think you may be over-thinking this. Here are a couple of ways (among many), where true means that the string contains at least one of the special characters):

#1

baddies = "[;`'<>-]"

pi = '3.14'
pi.delete(baddies).size < pi.size #=> false

pi = '3.1;4'
pi.delete(baddies).size < pi.size #=> true

#2

special = %w| [ ; ` ' < > - ] |
  # => ["[", ";", "`", "'", "<", ">", "-", "]"]

pi = '3.14'
(pi.chars & special).any? #=> false

pi = '3.1cat4'
(pi.chars & special).any? #=> false

pi = '3.1;4'
(pi.chars & special).any? #=> true
Cary Swoveland
  • 106,649
  • 6
  • 63
  • 100
0

You don't need to escape any of the characters in your character class:

special = "[;\`'<>-]"
regex = /#{special}/
p regex

#pi = ARGV[0]  #takes in console argument to test
pi = 'hello;world'

if pi == '3.1415926535897932385'
  puts "got it"
end

if pi =~ regex
  puts "stop word"
else 
  puts "incorrect"
end

--output:--
/[;`'<>-]/
stop word

And ARGV[0] is a string already. But, a shell/console also recognizes special characters when you enter them on the command line:

special = "[;\`'<>-]"
#regex = /[#{special.gsub(/./){|char| "\\#{char}"}}]/
regex = /#{special}/
p regex

pi = ARGV[0] #takes in console argument to test

if pi == '3.1415926535897932385'
  puts "got it"
end

if pi =~ regex
  puts "stop word"
else 
  puts "incorrect"
end

--output:--
~/ruby_programs$ ruby 1.rb ;
/[;`'<>-]/
incorrect

~/ruby_programs$ ruby 1.rb <
-bash: syntax error near unexpected token `newline'

If you want the shell/console to treat the special characters that it recognizes--as literals, then you have to quote them. There are various ways to quote things in a shell/console:

~/ruby_programs$ ruby 1.rb \;
/[;`'<>-]/
stop word

~/ruby_programs$ ruby 1.rb \<
/[;`'<>-]/
stop word

Note you can use String#[] too:

special = "[;\`'<>-]"
regex = /#{special}/
...
...
if pi[regex]
  puts "stop word"
else 
  puts "incorrect"
end
Community
  • 1
  • 1
7stud
  • 46,922
  • 14
  • 101
  • 127