开发者

How do I strip parenthesis from a string in Ruby?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜