Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
I have a Ruby narray automatically generated, which has this form: [x,y], where x and y are integers.
[x,y]
x
y
I want to transform [x,y] into this type of string:
"p\.x\.y"
I haven't succeeded in transforming it using a regular expression.
正则表达式用于匹配模式,而不是生成字符串。
要完成您想要的,只需使用 splat 运算符sprintf:
sprintf
array = [1,2] puts sprintf("p\\.%d\\.%d", *array)
这将输出"p\.1\.2"
"p\.1\.2"
替代方式:
x = [1,2] puts "p\\.#{x[0]}\\.#{x[1]}" #=> p\.1\2