Using Ruby's gsub with a string containing "\0"
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")
精彩评论