Removing text within parentheses and trailing spaces

79 Views Asked by At

I am using Rubular for this. I have the following string:

summary = "Hi world. Hi world. Hi world. Hi world. Hi world. Hi world. Hi world. Hi world (this is here). Hi world Hi world (wow)."

I'm trying to remove all parentheses from the string with the following:

summary.gsub!(/\([^()]*\)/,"")

The problem is this is not grabbing the space, so it results as follows:

"Hi world. Hi world. Hi world. Hi world. Hi world. Hi world. Hi world. Hi world . Hi world Hi world ."

Notice the undesired space before the period. How can I update the regex to remove the extra space that is being left over when I remove the parentheses?

1

There are 1 best solutions below

0
On BEST ANSWER

It's really just a minor modification to make it capture the spaces, too, if there are any:

summary.gsub!(/\s*\([^\)]*\)/, '')

That will only capture leading spaces. If you want leading and trailing:

summary.gsub!(/\s*\([^\)]*\)\s*/, '')