Quotation Mark in ruby regular expression misinterpreted as start of string
I have a macruby project in xcode, in which I want to replace all the left and right quotes in a string with ~@@~@@~"
and "~@@~@@~
, respectively. I tested the fol开发者_StackOverflow中文版lowing code in rubular.com, and it works correctly.
string.gsub!(/\B"/, "~@@~@@~\"")
string.gsub!(/\b"/, "\"~@@~@@~")
But when I use this in xcode, it interprets the "
in the regexp as the beginning of the string, and says I have the wrong numer of arguments for gsub
. I tried escaping the quote:
string.gsub!(/\B\"/, "~@@~@@~\"")
string.gsub!(/\b\"/, "\"~@@~@@~")
But that also did not work. Thanks for your help.
EDIT: I managed to get the error to go away, it seems it was due to something else. The highlighting is still off, but I can handle that, since it works.
Does the alternate syntax %r[\B\"]
work any better? What about Regexp.new
given a string?
精彩评论