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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help 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
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,958
    Thanks
    120
    Thanked 76 Times in 76 Posts
    replace first and last of "(" and ")"
    +
    Code:
    split(") (")
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search

  • #3
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Can you extend your answer please

    Thanks a lot , I'm not sure I understand your advice - how exactly should "(" and ")" be treated in the input for split().

  • #4
    New Coder
    Join Date
    Sep 2008
    Posts
    48
    Thanks
    7
    Thanked 0 Times in 0 Posts
    First, you would need to elimate the first and last brackets:

    s = s.substring(1,s.length()-1);

    Then, as the poster above said, you would split it by ") (":

    String[] sArray = s.split( ") (" );

    The array, sArray, would then contain the strings you mentioned

  • #5
    New Coder
    Join Date
    Jan 2011
    Location
    India
    Posts
    31
    Thanks
    0
    Thanked 4 Times in 4 Posts
    Splitting solution will be difficult. You can use the below code to do it..

    check it out

    PHP Code:
    public class Main {
        public static 
    void main(String[] args) {
            List 
    = new ArrayList();
            
    String str "(example) (ex) (am (p (k) t) le)";
            
    int openBracketCount 0;
            
            for(
    int i=0i<str.length();i++){
                
                
    char tok str.charAt(i);
                if(
    s.size()==0  && tok == ' ')
                    continue;
                if(
    tok == ')') {
                    
                    if(
    openBracketCount==1){
                        
    int size s.size();
                        for(
    int j=1;j<size;j++)
                            
    System.out.print(s.remove(1));
                        
    s.remove(0);
                        
    System.out.println();
                    }else if (
    openBracketCount<1){
                        
    System.out.println("Error in input.. Exiting..");
                        
    System.exit(0);
                    }else{
                        
    s.add(tok);
                    }
                    
    openBracketCount--;    
                }else{
                    if(
    tok == '(')
                        
    openBracketCount++;
                    
    s.add(tok);
                }
                
            }        
        }        

    Last edited by sandeep.e; 02-03-2011 at 09:55 AM.


  •  

    Posting Permissions

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