Alex Alex - 6 months ago 10
Ruby Question

Ruby: How do you escape from a regex with ']'

So I'm new to regex and I keep getting this warning and I don't know how to correct it:

madlib.rb:4: warning: regular expression has ']' without escape: /\[(\w*:*\w)]/


From this code:

keys=Hash.new { |h, k|
puts "Give me #{k.sub(/\[(\w*:*\w)]/, "")}:"
h[$1]=$stdin.gets.chomp

}
print keys
puts "", $*[0].split(".")[0].gsub("_", " "),
IO.read($*[0]).gsub(/\[(\w*:*\w)]/) { keys[$1] }


They way I call it seems simple at the moment, in the terminal I just put
madlib.rb Story1.txt
, but i'm going to be trying to implement it in a webpage using erb and sinatra, but I rather not have these warning pop up if I can help it.

Answer

Use \.

/\[(\w*:*\w)\]/

That should fix the issue

Comments