ruby 正则表达式中的引号被误解为字符串的开头
我在xcode中有一个macruby项目,我想在其中将字符串中的所有左右引号替换为 ~@@~@@~"
和 "~@@~@@分别是~
。我在 rubular.com 上测试了以下代码,它工作正常。
string.gsub!(/\B"/, "~@@~@@~\"")
string.gsub!(/\b"/, "\"~@@~@@~")
但是当我在 xcode 中使用它时,它会将正则表达式中的 "
解释为字符串的开头,并表示我的 gsub
参数数量错误。我尝试过转义引用:
string.gsub!(/\B\"/, "~@@~@@~\"")
string.gsub!(/\b\"/, "\"~@@~@@~")
但这也不起作用。
编辑:我设法让错误消失,似乎是由于其他原因导致的,但我可以处理这个问题。有用。
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 following 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
替代语法
%r[\B\"]
效果更好吗?给定一个字符串的Regexp.new
怎么样?Does the alternate syntax
%r[\B\"]
work any better? What aboutRegexp.new
given a string?