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.
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:
On Fri, Oct 1, 2010 at 7:20 PM, Frank Guerino <frank.guerino@traverseit.com> wrote:
--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
twitter: @hassan
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 Fri, Oct 1, 2010 at 9:20 PM, Frank Guerino <frank.guerino@traverseit.com>wrote:
Hi,
Given a string that contains one or more substrings within parenthesis,
such as:
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.
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.
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
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 Sat, Oct 2, 2010 at 9:25 AM, timr <timrandg@gmail.com> 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]