Ruby String: How do I strip anything between two parenthesis

Discussion in 'Ruby' started by Frank Guerino, Oct 2, 2010.

  1. 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
    --
    Posted via http://www.ruby-forum.com/.
     
    Frank Guerino, Oct 2, 2010
    #1
    1. Advertising

  2. On Fri, Oct 1, 2010 at 7:20 PM, Frank Guerino
    <> wrote:

    > 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'


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

    HTH,
    --
    Hassan Schroeder ------------------------
    twitter: @hassan
     
    Hassan Schroeder, Oct 2, 2010
    #2
    1. Advertising

  3. Frank Guerino

    Steel Steel Guest

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

    --
    Posted via http://www.ruby-forum.com/.
     
    Steel Steel, Oct 2, 2010
    #3
  4. Frank Guerino

    Josh Cheek Guest

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

    On Fri, Oct 1, 2010 at 9:20 PM, Frank Guerino
    <>wrote:

    > 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
    > --
    > 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?
     
    Josh Cheek, Oct 2, 2010
    #4
  5. Frank Guerino

    timr Guest

    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)
     
    timr, Oct 2, 2010
    #5
  6. Frank Guerino

    timr Guest

    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
     
    timr, Oct 2, 2010
    #6
  7. On Sat, Oct 2, 2010 at 9:25 AM, timr <> 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.
     
    Jesús Gabriel y Galán, Oct 2, 2010
    #7
  8. Frank Guerino

    w_a_x_man Guest

    On Oct 1, 9:20 pm, Frank Guerino <> wrote:
    > 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
    > --
    > 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"
     
    w_a_x_man, Oct 2, 2010
    #8
  9. substring = "something) else"

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

    Problems?
     
    Adam Prescott, Oct 2, 2010
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nathan Sokalski

    White strip showing up between two imagemaps

    Nathan Sokalski, Sep 22, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    372
    Nathan Sokalski
    Sep 23, 2006
  2. Nathan Sokalski

    White strip showing up between two imagemaps

    Nathan Sokalski, Sep 22, 2006, in forum: ASP .Net Building Controls
    Replies:
    4
    Views:
    150
    Nathan Sokalski
    Sep 23, 2006
  3. Nathan Sokalski

    White strip showing up between two imagemaps

    Nathan Sokalski, Sep 22, 2006, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    118
    Nathan Sokalski
    Sep 23, 2006
  4. Aquila
    Replies:
    35
    Views:
    454
    Mathieu Bouchard
    Mar 31, 2005
  5. yelipolok
    Replies:
    4
    Views:
    263
    John W. Krahn
    Jan 27, 2010
Loading...

Share This Page