Forwarding Forwarding - 3 months ago 9
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 "
(2002)
" part from it using Ruby. How is this done?

Answer

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+.