Bruce Xinda Lin Bruce Xinda Lin - 1 year ago 69
Ruby Question

Get content between { } braces

How can I get the content in between "{ }" in Ruby? For example,

I love {you}

How can I fetch the element "you"? If I want to replace the content, say change "you" to "her", how should I do that? Probably using

Answer Source

Regular expressions are the way to go with gsub. Something like:

existingString.gsub(/\{(.*?)\}/) { "her" }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download