如何使用 gsub 斜杠“/”带有反斜杠和斜杠“\/”在红宝石中

发布于 2024-08-22 19:40:08 字数 1017 浏览 5 评论 0原文

我尝试通过 ruby​​ gsub 命令将 "/foo/bar/dir" 修改为 "\/foo\/bar\/dir"

我在 irb 中测试它,结果是

x = "/foo/bar/dir"

x.gsub("/","\/")

=> "/foo/bar/dir"

x.gsub("/","\\/")

=> "\\/foo\\/bar\\/dir"

是否可以通过 gsub 将“/”替换为“/”?


问题来源:

我尝试执行“命令行中的字符串”,而“real_path”是我的 变量

real_path = "/home/me/www/idata"

path = real_path.gsub("/","\\/")

=> \\/home\\/me\\/www\\/idata 

# But what I expect is \/home\/me\/www\/idata

run "sed 's/SHARE_PATH/#{path}/g' #{path}/config/sphinx.yml > #{path}/config/sphinx.tmp.yml"

“run”命令的

"sh -c 'sed '\''s/SHARE_PATH/\\/home\\/me\\/www\\/idata\\/shared/g .... "

结果是我只需要一个反斜杠,就像

"sh -c 'sed '\''s/SHARE_PATH/\/home\/me\/www\/idata\/shared/g .... "

“run”是来自 Capistrano 的命令

我的解决方案是

使用单引号而不是像这样的双引号

path = real_path.gsub("/",'\/') 

I try to modify "/foo/bar/dir" to "\/foo\/bar\/dir" by ruby gsub command.

I test it in irb the result is

x = "/foo/bar/dir"

x.gsub("/","\/")

=> "/foo/bar/dir"

x.gsub("/","\\/")

=> "\\/foo\\/bar\\/dir"

Is it possible to replace "/" with "/" by gsub ?


Source of problems:

I try to execute "string in command line" and "real_path" is my variable

real_path = "/home/me/www/idata"

path = real_path.gsub("/","\\/")

=> \\/home\\/me\\/www\\/idata 

# But what I expect is \/home\/me\/www\/idata

run "sed 's/SHARE_PATH/#{path}/g' #{path}/config/sphinx.yml > #{path}/config/sphinx.tmp.yml"

result from "run" command is

"sh -c 'sed '\''s/SHARE_PATH/\\/home\\/me\\/www\\/idata\\/shared/g .... "

I need is only one back slash like

"sh -c 'sed '\''s/SHARE_PATH/\/home\/me\/www\/idata\/shared/g .... "

"run" is command from Capistrano

my solution is

use single quote instead of double quote like this

path = real_path.gsub("/",'\/') 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如日中天 2024-08-29 19:40:08

你已经写道:

x = "/foo/bar/dir"
x.gsub("/","\\/")
=> "\\/foo\\/bar\\/dir"

所以你做了你之前要求的事情。 x.gsub("/","\\/") 实际上计算结果为 "\/foo\/bar\/dir" 但 irb 打印返回值 < code>inspect 方法而不是 to_s

编辑:你的意思是

real_path.gsub("/","\/")

不是无论如何

real_path.gsub("\/","\/")

输出是正确的 - 你用\/改变了/所以你有

"sh -c 'sed '\''s/SHARE_PATH/\/home\/me\/www\/idata\/shared/g'\'' .... "`

而不是

`"sh -c 'sed '\''s/SHARE_PATH//home/me/www/idata/shared/g'\'' .... "`

并且结果与irb 的结果(注意缺少双反斜杠)。

对于路径操作,我建议使用 File.join文档< /a>)

顺便问一下:为什么要这样修改路径? (1)

Edit2: 为什么您要求将“/”更改为“/”,但要写以下行?

path = real_path.gsub("\/","\\/") 

您想实现什么目标?您对问题(1) 的答案是什么?

编辑3:

开始:

>> real_path = "/foo/bar/dir"
=> "/foo/bar/dir"
>> path = real_path.gsub("/", "\\/")
=> "\\/foo\\/bar\\/dir"
>> puts "sed 's/SHARE_PATH/#{path}/g' #{path}/config/sphinx.yml > #{path}/config/sphinx.tmp.yml"
sed 's/SHARE_PATH/\/foo\/bar\/dir/g' \/foo\/bar\/dir/config/sphinx.yml > \/foo\/bar\/dir/config/sphinx.tmp.yml
=> nil
>>

但我不明白为什么你在路径中需要反斜杠?

You have written:

x = "/foo/bar/dir"
x.gsub("/","\\/")
=> "\\/foo\\/bar\\/dir"

so You did what You had asked before. x.gsub("/","\\/") in fact evaluates to "\/foo\/bar\/dir" but irb prints return value of inspect method instead of to_s.

Edit: Did You mean

real_path.gsub("/","\/")

istead of

real_path.gsub("\/","\/")

Anyway the output is correct - You changed / with \/ so You have

"sh -c 'sed '\''s/SHARE_PATH/\/home\/me\/www\/idata\/shared/g'\'' .... "`

instead of

`"sh -c 'sed '\''s/SHARE_PATH//home/me/www/idata/shared/g'\'' .... "`

and result is different from irb's result (notice the lack of doubled backslash).

For path manipulation I recommend using File.join (documentation)

By the way: why are You modifying the path this way? (1)

Edit2: Why are You asking about changing "/" to "/" but write the following line?

path = real_path.gsub("\/","\\/") 

What are You trying to achieve? And what is Your answer to question (1) ?

Edit3:

Here We go:

>> real_path = "/foo/bar/dir"
=> "/foo/bar/dir"
>> path = real_path.gsub("/", "\\/")
=> "\\/foo\\/bar\\/dir"
>> puts "sed 's/SHARE_PATH/#{path}/g' #{path}/config/sphinx.yml > #{path}/config/sphinx.tmp.yml"
sed 's/SHARE_PATH/\/foo\/bar\/dir/g' \/foo\/bar\/dir/config/sphinx.yml > \/foo\/bar\/dir/config/sphinx.tmp.yml
=> nil
>>

but I do not understand why You need backslash in a path?

源来凯始玺欢你 2024-08-29 19:40:08

是的,

irb(main):028:0> (t = x.gsub("/", "\\/")) && nil
=> nil
irb(main):029:0> t
=> "\\/foo\\/bar\\/dir"
irb(main):030:0> puts t
\/foo\/bar\/dir
=> nil

您的第一个示例实际上做了您想要的事情,但是 irb 使用的 .inspect 方法正在转义反斜杠,因此看起来还有额外的内容。如果您使用了puts,您就会看到真实的结果。

Yes

irb(main):028:0> (t = x.gsub("/", "\\/")) && nil
=> nil
irb(main):029:0> t
=> "\\/foo\\/bar\\/dir"
irb(main):030:0> puts t
\/foo\/bar\/dir
=> nil

Your first example actually did what you wanted, but the .inspect method that irb is using is escaping backslashes, so it looked like there were extras. If you had used puts you would have seen the real result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文