简体   繁体   English

gsub中的简单正则表达式

[英]Simple regular expression in gsub

I have a little problem. 我有一点问题。 I have "some text 23%", but I need "some text ; 23%", so I tried replacing " " for " ; ". 我有“一些文本23%”,但是我需要“一些文本; 23%”,因此我尝试将“;”替换为“。”。

I tried something like this: 我尝试过这样的事情:

"some text 23%".gsub /(\d+)%/, "; \0"

Problem is in response, in console it is ok, but when I use it in script I have: 问题在于响应,在控制台中可以,但是当我在脚本中使用它时,我有:

"some text ; ^@"

And I don't know what do with ^@ for have good value. 而且我不知道^ @具有很好的价值。

Thanks for help! 感谢帮助!

The following should work: 以下应该工作:

"some text 23%".gsub(/(\d+)%/, "; \\1%")

You can find more information about making back references to the captures in the ruby documentation: http://ruby-doc.org/core-1.9.3/String.html#method-i-gsub 您可以在ruby文档中找到有关回溯捕获的更多信息: http : //ruby-doc.org/core-1.9.3/String.html#method-i-gsub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM