本文介绍了在Ruby中的字符串中转义双重和单反斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正尝试以这样一种格式访问Windows平台上的ruby脚本中的网路路径。

I'm trying to access a network path in my ruby script on a windows platform in a format like this.

\\servername\some windows share\folder 1\folder2\

现在如果我试图用这个路径,它不会工作。单个反斜杠未正确转义此脚本。

Now If I try to use this as a path, it won't work. Single backslashes are not properly escaped for this script.

path = "\\servername\some windows share\folder 1\folder2\"
d = Dir.new(path)

我可以想到要正确逃脱路上的斜线。但是我无法逃避那个单反斜杠 - 因为它是特殊的意思。我尝试使用单引号,双引号,转义反斜杠本身,使用替代引号如%Q {}或%q {},使用ascii进行char转换。在某种意义上说,我没有做到这一点,没有什么可行的。 :-)现在,临时解决方案是映射一个网络驱动器N:\指向该路径并以这种方式访问​​,但这不是解决方案。

I tried everything I could think of to properly escape slashes in the path. However I can't escape that single backslash - because of it's special meaning. I tried single quotes, double quotes, escaping backslash itself, using alternate quotes such as %Q{} or %q{}, using ascii to char conversion. Nothing works in a sense that I'm not doing it right. :-) Right now the temp solution is to Map a network drive N:\ pointing to that path and access it that way, but that not a solution.

任何人都有任何想法如何正确逃脱单反斜杠?

Does anyone have any idea how to properly escape single backslashes?

谢谢

推荐答案

只需将每个反斜杠加倍,如下所示:

Just double-up every backslash, like so:

"\\\\servername\\some windows share\\folder 1\\folder2\\"

这篇关于在Ruby中的字符串中转义双重和单反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 23:42