Ruby String: How do I strip anything between two parenthesis

Hi,

Given a string that contains one or more substrings within parenthesis,
such as:

myString = ‘This is (variable length substring #1) (variable length
substring #2) string’

How would I effectively strip out anything between any two parenthesis
and replace it with a blank (eliminating such substrings) such that it
would look like:

myString = ‘This is a string’

I’m assuming the answer would be a combination of the string “remove”
command and the use of regular expressions but can’t seem to get it to
work.

Thanks for any help you can offer.

My Best,

Frank

On Fri, Oct 1, 2010 at 7:20 PM, Frank G.
[email protected] wrote:

myString = ‘This is a string’

myString
=> “This is (variable length substring #1) (variable length\nsubstring
#2) string”

myString.gsub(/([^)]+)\s+/,’’)
=> “This is string”

Interpolating an “a” in there is another story :slight_smile:

HTH,

I’m assuming the answer would be a combination of the string “remove”
command and the use of regular expressions but can’t seem to get it to
work.

Non regex way

irb(main):027:0> myString.split(")").collect{|x| x.split("(")[0]}.join
=> “This is string”

I’d go with regex, but we need more cases with expected results to
figure out which regex would work for you.
How about this?

“no match outside (inside all (gets) collected)”.match(/((.*))/)[1]
=> “inside all (gets) collected”

“no match outside (lisp? (atom? (car (me_a_string 3 4)))”.match(/((.*))/)[1]
=> “lisp? (atom? (car (me_a_string 3 4))”

Explanation:
/((.*))/

/ <- regex syntax -> / means look for a pattern matching
( an open parenthesis (the backslash is working as an escape
character here)
(.*) means as many characters as you can find (even none is okay)
and store the hit
) a close parenthesis (again backslash is just an escape character)

By the way, one of my favorite books of all time is O’reilly book on
Mastering Regular Expressions. We should have a holiday where everyone
gets time off of work to read that book.
Tim

On Fri, Oct 1, 2010 at 9:20 PM, Frank G.
[email protected]wrote:

would look like:

Frank

Posted via http://www.ruby-forum.com/.

What if your substrings have parentheses in them? How do you know, when
you
come to a parenthesis, whether it terminates your substring, or is a
character within your substring?

Example:
myString = ‘1(a)2(b)3’

That could be either “1(substring)3”
where substring is “a)2(b”

Or it could be “123”
with two substrings of “a” and “b”

How do you know which it supposed to be?

On Sat, Oct 2, 2010 at 9:25 AM, timr [email protected] wrote:

I’d go with regex, but we need more cases with expected results to
figure out which regex would work for you.
How about this?

“no match outside (inside all (gets) collected)”.match(/((.*))/)[1]
=> “inside all (gets) collected”

This could match more than expected:

irb(main):001:0> “something (this should be stripped), but not this,
(and this too), and again not this”.match(/((.*))/)[1]
=> “this should be stripped), but not this, (and this too”

Jesus.

On Oct 1, 9:20 pm, Frank G. [email protected] wrote:

would look like:

Frank

Posted viahttp://www.ruby-forum.com/.

my_string = “This is (variable length substring #1) " +
“(variable length substring #2) string”
my_string.gsub( /(.*?)/, " " ).squeeze(” ")
==>“This is string”

substring = “something) else”

my_string = “This is (#{substring}) string”

Problems?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs