Results 1 to 10 of 10
  1. #1
    rtaig is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default Help needed with String.split()

    Hi

    I'd like to split a String with the following rule : Parse everything between a "( )" parenthesis besides nested parenthesis, meaning:

    String s = "(example) (ex) (am (p (k) t) le)"

    needs to be parsed into :
    example
    ex
    am (p (k) t) le

    I tried to work with split() and regular expressions but couldn't find the right one.

    Thanks in advance:)

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,563
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by rtaig View Post
    Hi

    I'd like to split a String with the following rule : Parse everything between a "( )" parenthesis besides nested parenthesis, meaning:

    String s = "(example) (ex) (am (p (k) t) le)"

    needs to be parsed into :
    example
    ex
    am (p (k) t) le

    I tried to work with split() and regular expressions but couldn't find the right one.
    Regular expressions can't do that; in normal terms without mathematical proof: regular expressions can't count. You have to parse your String 'manually'.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by JosAH View Post
    Regular expressions can't do that; in normal terms without mathematical proof: regular expressions can't count. You have to parse your String 'manually'.
    And he'll probably want to use a stack to do this, right?

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,563
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Fubarable View Post
    And he'll probably want to use a stack to do this, right?
    Formally yes, practically a simple counter will do fine ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by JosAH View Post
    Formally yes, practically a simple counter will do fine ;-)
    As always, thanks!

  6. #6
    rtaig is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default

    Thanks , I know that a full parsing of nested parenthesis can't be done with regular expressions but please note that my demand is much simpler and doesn't involve counting - I'm saying : "parse everything inside a parenthesis accept if it's inside other parenthesis - i don't care how many " - to be clear :

    String s = "(I) (don't) (care)" should give : [I , don't , care];
    String s = (I) (do (n'(t c) a) (re) should give: [I, do (n'(t c) a, re;

    Is this equivalent hard as the general task ?

    Thanks

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by rtaig View Post
    my demand is much simpler and doesn't involve counting
    Don't you love it when n00bs refute the excellent advice provided by much more knowledgable people.

    So your problem doesn't involve counting huh, so how does it know how many brackets to ignore? Using your original example "(am (p (k)" satisfies your requirements.

  8. #8
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    @rtaig, this is toy example
    Java Code:
                    String s  = "(I) (don't) (care)";
                    int c = 0;
                    String joiner = "";
                    for(int i =0 ;i <s.length(); i++){
                            if ( c!= 0 ){
                                    joiner+=s.charAt(i);
                            }else if ( c==0 && joiner !="" ){
                                    System.out.println ("joiner: " + joiner);
                                    joiner="";
                            }
                            if ( s.charAt(i) == '(' ){
                                    c++;
                            }else if ( s.charAt(i) == ')' ) {
                                    c--;
                            }
                    }
                    System.out.println("joiner: " + joiner);

  9. #9
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Quote Originally Posted by rtaig View Post
    Thanks , I know that a full parsing of nested parenthesis can't be done with regular expressions but please note that my demand is much simpler and doesn't involve counting - I'm saying : "parse everything inside a parenthesis accept if it's inside other parenthesis - i don't care how many " - to be clear :

    String s = "(I) (don't) (care)" should give : [I , don't , care];
    String s = (I) (do (n'(t c) a) (re) should give: [I, do (n'(t c) a, re;

    Is this equivalent hard as the general task ?

    Thanks
    If you will not use a counting then the result will be
    which is different from your expected output:
    INPUT:
    String s = (You) (are (no (t li) st) (ening)
    POSSIBLE OUTPUT: [You, are not li st, ening];

    INPUT:
    String s = (You) (are (no (t li) st) (ening)
    OUTPUT: [You, are (no(t li) st, ening];

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,563
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by rtaig View Post
    Thanks , I know that a full parsing of nested parenthesis can't be done with regular expressions but please note that my demand is much simpler and doesn't involve counting
    It does involve counting; this morning I tried to come up with a simple proof that parsing balanced parentheses can't be done with regular languages but I failed. I always fall back to the pumping lemmas which show that the language of balanced (nested) parentheses is a context free language; but then again, I didn't have my coffee yet and I'm going to fix that right now ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Split a String with split()--Help
    By danilson in forum New To Java
    Replies: 7
    Last Post: 11-19-2010, 04:08 PM
  2. Split string help
    By Ben1 in forum New To Java
    Replies: 1
    Last Post: 11-08-2010, 04:28 PM
  3. String Split
    By sarovarc in forum New To Java
    Replies: 6
    Last Post: 04-19-2010, 05:06 AM
  4. How to split a String using split function
    By Java Tip in forum java.lang
    Replies: 4
    Last Post: 04-17-2009, 08:27 PM
  5. How to split a String using split function
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:32 PM

Posting Permissions

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