开发者

How do I strip parenthesis from a string in Ruby?

开发者 https://www.devze.com 2023-04-07 12:18 出处:网络
I have a string, like this: \"yellow-corn-(corn-on-the-cob)\" and I would like to strip the parenthesis from the string to get something like this:

I have a string, like this:

"yellow-corn-(corn-on-the-cob)"

and I would like to strip the parenthesis from the string to get something like this:

"yellow-corn-corn-on-the-cob"

I believe you would use gsub to accomplish this, but I'm not sure what pattern I would need to match the pa开发者_运维问答renthesis. Something like:

clean_string = old_string.gsub(PATTERN,"")


Without regular expression:

"yellow-corn-(corn-on-the-cob)".delete('()') #=> "yellow-corn-corn-on-the-cob"


Try this:

clean_string = old_string.gsub(/[()]/, "")

On a side note, Rubular is awesome to test your regular expressions quickly.

0

精彩评论

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