开发者

Using Ruby's gsub with a string containing "\0"

开发者 https://www.devze.com 2023-01-09 04:24 出处:网络
I\'m having trouble using gsub correctly: Given this code: \"replace me\".gsub(/replace me/, \"this \\\\0 is a test\")

I'm having trouble using gsub correctly:

Given this code:

"replace me".gsub(/replace me/, "this \\0 is a test")

The result is:

 "this replace me is a test" 

But what I am expecting is:

"this \0 is a test"开发者_运维问答

How do I use gsub to get the result I want?


Escape it with another backslash so that gsub will know you want "\\0".

"replace me".gsub(/replace me/, "this \\\\0 is a test")

(Edit) if by "\0" you meant the byte 0x00, do this:

"replace me".gsub(/replace me/, "this \0 is a test")
0

精彩评论

暂无评论...
验证码 换一张
取 消