Reputation: 48453
I have these kind of strings:
A regular sentence.
A regular sentence (United Kingdom).
A regular sentence (UK).
The goal is to remove the term in the brackets, thus the desired output would be:
A regular sentence.
A regular sentence.
A regular sentence.
How to achieve this in Ruby (probably with using regular expressions?)?
Thank you
Upvotes: 0
Views: 90
Reputation: 13901
In case the sentence itself can contain parenthesis:
a = "A (very) regular sentence (UK)."
p a.gsub(/\s\([^()]*\)(?=\.\Z)/, '') #=> "A (very) regular sentence."
Upvotes: 0
Reputation: 11244
"A regular sentence (UK).".gsub(/\(.*\)/,"").strip #=> "A regular sentence ."
Upvotes: 0