开发者

Dereferencing variables within ruby regex

I am having trouble figuring this one out. I need to be able to dereference an 开发者_高级运维integer variable in ruby within a regex. So, for example, lets say I have a string called this_string, and I want to insert a space every 8 characters, this would work:

this_string.scan(/.{8}|.+/).join(" ")

But how do I do this every N characters using a regex, where N is an arbitrary integer?


n = 3
s.scan(Regexp.new ".{#{n}}|.+").join ' '
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜