mercredi 25 mars 2015

java-Match a regex pattern in a string(listing matching and non-matching parts)

i have a csv string containing five fields(3 mandatory and 2 optional). The optional fields are in posiition 3 and 5. I have a regex pattern to identify the strings in double quotes



private static final String TEXT_IN_DBL_QUOTES_REGEX="\"([^\"]*)\"";
private static final String rowData="\"Field1\", \"Field2\", , \"Field4\", ";

public static void main(String[] args){
List<String> fields = Lists.newArrayList();
Pattern regex = Pattern.compile(TEXT_IN_DBL_QUOTES_REGEX);
Matcher matcher = regex.matcher(rowData);
while(matcher.find()){
fields.add(matcher.group(1));
}
return fields;
}

My expected result is an array list containing
Field1, Field2, null, Field4, null
but the above is returning
Field1, Field2, Field4


How can i tweak the above code to get the desired result?


Aucun commentaire:

Enregistrer un commentaire