简体   繁体   中英

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 " ; ".

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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