Click to See Complete Forum and Search --> : Split string with double quotes
lornen
October 11th, 2009, 10:57 PM
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:
dog cat "elephant giraffe" hippo
I got the following array:
[0] = dog
[1] = cat
[2] = "elepant
[3] = giraffe"
[4] = hippo
But I need this:
[0] = dog
[1] = cat
[2] = elepant giraffe
[3] = hippo
Or maybe some other alternatives to do the splitting?
My current 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.
nuzzle
October 12th, 2009, 01:50 AM
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.
lornen
October 12th, 2009, 03:20 AM
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/1441556/parsing-csv-input-with-a-regex-in-java/1553310#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.
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:
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
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.