Forwarding Forwarding - 1 year ago 76
Ruby Question

How remove "(2002)" (without quotes) from string in Ruby?

I have a string like this

This is some text; Awesome! (2002)

I want to remove the "
" part from it using Ruby. How is this done?

Answer Source

To remove any amount of whitespace symbols followed with a (, then one or more digits and a ) at the end of the string, use a sub with a /\s*\(\d+\)\z/ regex:

"This is some text; Awesome! (2002)".sub(/\s*\(\d+\)\z/,"")

See Ruby demo

If you mean a literal 2002, use it instead of \d+.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download