Ruby String: How do I strip anything between two parenthesis

F

Frank Guerino

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
 
H

Hassan Schroeder

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'
=> "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 :)

HTH,
 
S

Steel Steel

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"
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

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
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?
 
T

timr

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)
 
T

timr

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
 
J

Jesús Gabriel y Galán

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

w_a_x_man

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

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

Adam Prescott

substring = "something) else"

my_string = "This is (#{substring}) string"

Problems?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,001
Messages
2,570,255
Members
46,856
Latest member
MyronKatz6

Latest Threads

Top