CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2009
    Posts
    3

    Smile Split string with double quotes

    Hi,

    I'm not a regex expert, and I can't seem to find the solution for this (need to work on my searching skills and regex ).
    How can I split the input string entered into string array?

    i.e.
    From:
    Code:
    dog cat "elephant giraffe" hippo
    I got the following array:
    Code:
    [0] = dog
    [1] = cat
    [2] = "elepant 
    [3] = giraffe"
    [4] = hippo
    But I need this:
    Code:
    [0] = dog
    [1] = cat
    [2] = elepant giraffe
    [3] = hippo
    Or maybe some other alternatives to do the splitting?

    My current code:
    Code:
    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);
    String input= br.readLine().trim();
    String[] tokens = input.split("\\s+)"; // regex will not handle spaces enclosed by quotes.
    Thanks in advance.

  2. #2
    Join Date
    May 2009
    Posts
    2,413

    Re: Split string with double quotes

    Quote Originally Posted by lornen View Post
    I'm not a regex expert, and I can't seem to find the solution for this (need to work on my searching skills and regex ).
    Why don't you write an ordinary simple parser? It's just a few lines of code.

  3. #3
    Join Date
    Oct 2009
    Posts
    3

    Red face Re: Split string with double quotes

    Hi,

    My brain cannot reinvent the wheel on a Monday afternoon, and too busy today to do so

    I've done a very slight modification of the code from http://stackoverflow.com/questions/1...553310#1553310

    The below code will split the input string by the whitespace, and ignore whitespace that are enclosed by double quotes(Not going into single quotes now...). It works for my requirement, even though there are certainly better alternatives out there.

    Code:
    package common;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class CSVParser {
    
        /*
         * This Pattern will match on either quoted text or text between space, including
         * whitespace, and accounting for beginning and end of line.
         */
        //private final Pattern csvPattern = Pattern.compile("\"([^\"]*)\"|(?<=,|^)([^,]*)(?:,|$)");
        private final Pattern csvPattern = Pattern.compile("\"([^\"]*)\"|(?<= |^)([^ ]*)(?: |$)");  
        private ArrayList<String> allMatches = null;        
        private Matcher matcher = null;
        private String match = null;
    
        public CSVParser() {                
            allMatches = new ArrayList<String>();
            matcher = null;
            match = null;
        }
    
        public String[] parse(String csvLine) {
            matcher = csvPattern.matcher(csvLine);
            allMatches.clear();
            String match;
            while (matcher.find()) {
                match = matcher.group(1);
                if (match!=null) {
                        allMatches.add(match);
                }
                else {
                        allMatches.add(matcher.group(2));
                }
            }
            List<String> results = new ArrayList<String>();
    		for(int i = 0 ; i < allMatches.size(); i++ ) {
    			if(!allMatches.get(i).equals("")) {
    				results.add(allMatches.get(i).trim());
    			}
    		}
            if (results.size() > 0) {
                    return results.toArray(new String[results.size()]);
            }
            else {
                    return new String[0];
            }                       
        }   
    }
    testing:
    Code:
    Enter you command to split:
    apple banana orange
    String Array:
    0:  apple
    1:  banana
    2:  orange
    Enter you command to split:
    apple    "banana orange" gorilla
    String Array:
    0:  apple
    1:  banana orange
    2:  gorilla
    Enter you command to split:
    apple "banana orange
    String Array:
    0:  apple
    1:  "banana
    2:  orange

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured