Forwarding Forwarding - 7 months ago 21
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?


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