Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    New Coder
    Join Date
    Jan 2010
    Location
    Milkyway Galaxy
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Regex: Removing Nested Parentheses

    I simply cannot figure this out, so I figure I'd ask the pros for some help

    Is it possible to use a regular expression to replace infinitely-nested parentheses and their contents with " " (a blank space)? For example:

    (This (is (a (sentence) with) lots of nested) parentheses)

    Thanks
    Last edited by neochronomo; 07-23-2010 at 09:29 AM.

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Like this?
    Code:
    <script type="text/javascript">
    var str='(This (is (a (sentence) with) lots of nested) parentheses)';
    str=str.replace(/\(|\)/g,'');
    alert(str);
    </script>
    It is not very clear what you want to replace.

    More about Regular Expressions:
    http://lawrence.ecorp.net/inet/samples/regexp-intro.php
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    New Coder
    Join Date
    Jan 2010
    Location
    Milkyway Galaxy
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts
    I'll try to be more clear, with another example.

    I want this:
    Four score (and seven (years) ago) our fathers brought (forth onto) this continent

    To turn into this:
    Four score our fathers brought this continent


    In other words, I want all parenthetical information removed; the parentheses and what they contain, even if nested.

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Yes. Interesting problem. First time I thought it is simple - a non-greedy RegExp:
    Code:
    <script type="text/javascript">
    var str='Four score (and seven (years) ago) our fathers brought (forth onto) this continent';
    str=str.replace(/\(.*?\)/g,'');
    </script>
    But that should work only in case there are no other inner parenthesis nested inside other parenthesis, so that in your case the result is:

    'Four score ago) our fathers brought this continent'

    Which is logical, but it is not what we need.

    I promise to think more about that. And maybe someone else here sees a complete solution...
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    I think I found a way. I am not sure it is the best possible solve, but it works, as far as I have seen:
    Code:
    <script type="text/javascript">
    var str='Four score (and seven (years) ago) our fathers brought (forth onto) this continent';
    while(str.match(/\(.*?\)/g)){
    str=str.replace(/\(.[^\(]*?\)/g,'');
    }
    </script>
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    And probably you need to remove the extra single spaces as well:
    Code:
    <script type="text/javascript">
    var str='Four score (and seven (years) ago) our fathers brought (forth onto) this continent';
    while(str.match(/\(.*?\)/g)){
    str=str.replace(/\(.[^\(]*?\)/g,'');
    }
    str=str.replace(/\s\s/g,' ');
    </script>
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    And perhaps perform a check for unmatched parentheses:-


    Code:
    <script type="text/javascript">
    function removeParentheses() {
    
    var str = 'Four score (and seven (years) ago) our fathers brought (forth onto) this continent';
    
    var c = 0;
    var n = 0;
    for (var j=0 ; j < str.length ; j++) { 
    c = str.charAt(j);
    if (c == "(") {n++}
    if (c == ")") {n--}
    }
    if (n != 0) {
    alert ("Error - unmatched parentheses");
    return false;
    }
    
    while(str.match(/\(.*?\)/g)){
    str=str.replace(/\(.[^\(]*?\)/g,'');
    }
    str=str.replace(/\s\s/g,' ');
    alert (str);
    }
    
    removeParentheses();
    </script>

  • Users who have thanked Philip M for this post:

    Kor (07-23-2010)

  • #8
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Good idea, Philip.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,857
    Thanks
    78
    Thanked 4,417 Times in 4,382 Posts
    Hmmm....but what about this:
    Code:
    What ) about parens in text like ( this?
    Your counting will find they are matched and yet nothing will be removed via the regexp's.

    How about K.I.S.S.?? Just check for hangers-on *after* the replacement of matches:

    Code:
    <script type="text/javascript">
    function removeParentheses() 
    {
    
        var str = 'Four ) score (and seven (years) ago) our fathers brought (forth onto) this ( continent';
    
        while(str.match(/\(.*?\)/g))
        {
            str = str.replace(/\(.[^\(]*?\)/g,'');
        }
        str=str.replace(/\s+/g,' ');
    
        if ( str.match(/[\(\)]/) ) alert("Mismatched parentheses?");
    
        alert(str);
    }
    
    removeParentheses();
    </script>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,857
    Thanks
    78
    Thanked 4,417 Times in 4,382 Posts
    A minor simplification that should help performance a tiny bit:
    Code:
    <script type="text/javascript">
    function removeParentheses(str) 
    {
        while( true )
        {
            var str2 = str.replace(/\(.[^\(]*?\)/g,'');
            if ( str2 == str ) break;
            str = str2;
        }
        str = str.replace(/\s+/g,' ');
    
        if ( str.match(/[\(\)]/) ) alert("Mismatched parentheses?");
        return str;
    }
    
    var test1 = 'Four score (and seven (years) ago) our fathers brought (forth onto) this continent';
    alert( removeParentheses(test1) );
    
    alert( removeParentheses("and ) simple ( test ) like this ( as well ") );
    
    </script>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,857
    Thanks
    78
    Thanked 4,417 Times in 4,382 Posts
    By the by, doing
    Code:
        str = str.replace(/\s\s/g, " ");
    will *NOT* remove all extra spaces. For example, if there are 3 spaces (or more) in a row, that will consolidate each *pair* of spaces down to one, but the replaced text is not rescanned. So, for example, 9 spaces in a row would be consolidated to 5 spaces.

    That's why I do
    Code:
        str = str.replace(/\s+/g, " ");
    Probably even better would be
    Code:
        str = str.replace( /\s{2,}/g, " ");
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #12
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    // I'm late to the party, but this method may do-

    Code:
    function stripParenths(s){
    	var rx=/\s*\([^)(]*\)\s*/;
    	while(rx.test(s)){
    		s= s.split(rx).join(' ');
    	}
    	return s;
    	// or to remove any stray parens:  return s.replace(/[)(]/g,'');
    }
    //test
    Code:
    var str= 'Four score (and (seven (years)) ago) our fathers brought (forth unto) this continent';
    alert(stripParenths(str))
    /* returned value: (String)
    Four score our fathers brought this continent
    */

  • #13
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,857
    Thanks
    78
    Thanked 4,417 Times in 4,382 Posts
    Makes me nervous.

    Look at the value of s after each iteration of the loop.

    Surprise.

    Can't help but wondering if there is some set of parens that would fool that code.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #14
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by Old Pedant View Post
    Hmmm....but what about this:
    Code:
    What ) about parens in text like ( this?
    Your counting will find they are matched and yet nothing will be removed via the regexp's.
    Perhaps that is what is desired.

    var str = "What :-) about parens in text like 8-( this?"


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •