Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Ruby String: How do I strip anything between two parenthesis

Reply
Thread Tools

Ruby String: How do I strip anything between two parenthesis

 
 
Frank Guerino
Guest
Posts: n/a
 
      10-02-2010
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/.

 
Reply With Quote
 
 
 
 
Hassan Schroeder
Guest
Posts: n/a
 
      10-02-2010
On Fri, Oct 1, 2010 at 7:20 PM, Frank Guerino
<(E-Mail Removed)> 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 ------------------------ http://www.velocityreviews.com/forums/(E-Mail Removed)
twitter: @hassan

 
Reply With Quote
 
 
 
 
Steel Steel
Guest
Posts: n/a
 
      10-02-2010
> 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/.

 
Reply With Quote
 
Josh Cheek
Guest
Posts: n/a
 
      10-02-2010
[Note: parts of this message were removed to make it a legal post.]

On Fri, Oct 1, 2010 at 9:20 PM, Frank Guerino
<(E-Mail Removed)>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?

 
Reply With Quote
 
timr
Guest
Posts: n/a
 
      10-02-2010
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)
 
Reply With Quote
 
timr
Guest
Posts: n/a
 
      10-02-2010
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
 
Reply With Quote
 
Jesús Gabriel y Galán
Guest
Posts: n/a
 
      10-02-2010
On Sat, Oct 2, 2010 at 9:25 AM, timr <(E-Mail Removed)> 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.

 
Reply With Quote
 
w_a_x_man
Guest
Posts: n/a
 
      10-02-2010
On Oct 1, 9:20*pm, Frank Guerino <(E-Mail Removed)> 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"
 
Reply With Quote
 
Adam Prescott
Guest
Posts: n/a
 
      10-02-2010
substring = "something) else"

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

Problems?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
strip all but second second line from bottom and then strip that!!!! yelipolok Perl Misc 4 01-27-2010 08:14 AM
White strip showing up between two imagemaps Nathan Sokalski ASP .Net Building Controls 4 09-22-2006 11:53 PM
White strip showing up between two imagemaps Nathan Sokalski ASP .Net 4 09-22-2006 11:53 PM
strip and its evil brother strip! Aquila Ruby 35 03-31-2005 04:10 AM



Advertisments