I have this kind of CSV file which I have to parse in Java.
2012-11-01 00, 1106, 2194.1971066908
2012-11-01 01, 760, 1271.8460526316
.
.
.
2012-11-30 21, 1353, 1464.0014781966
2012-11-30 22, 1810, 1338.8331491713
2012-11-30 23, 1537, 1222.7826935589
720 rows selected.
Elapsed: 00:37:00.23
This is Java code I created in order to segregate each column and store it in a list.
public void extractFile(String fileName){
try{
BufferedReader bf = new BufferedReader(new FileReader(fileName));
try {
String readBuff = bf.readLine();
while (readBuff!=null){
Pattern checkData = Pattern.compile("[a-zA-Z]");
Matcher match = checkData.matcher(readBuff);
if (match.find()){
readBuff = null;
}
else if (!match.find()){
String[] splitReadBuffByComma = new String[3];
splitReadBuffByComma = readBuff.split(",");
for (int x=0; x<splitReadBuffByComma.length; x++){
if (x==0){
dHourList.add(splitReadBuffByComma[x]);
}
else if (x==1){
throughputList.add(splitReadBuffByComma[x]);
}
else if (x==2){
avgRespTimeList.add(splitReadBuffByComma[x]);
}
}
}
readBuff = bf.readLine();
}
}
finally{
bf.close();
}
}
catch(FileNotFoundException e){
System.out.println("File not found dude: "+ e);
}
catch(IOException e){
System.out.println("Error Exception dude: "+e);
}
}
The problem is that the regex I created is a bit faulty because it still includes the text "720 rows selected" and stores them in dHourList.
dHourList should only store the date column represented like this "2012-11-01 00…etc"
throughputList = "1106, 760 …etc"
avgResponseTime = "2194.192, 1271.846…etc"
What should be the correct reg expression for this?
Update
2012-11-30 21
2012-11-30 22
2012-11-30 23
720 rows selected.
Elapsed: 00:37:00.23
Size of date-hour: 724 size of throughput: 720 size of avg resp time: 720
I used this in checkData regex instead because if I use one slash \d compile will say invalid escape sequence
Pattern checkData = Pattern.compile("^(19|20)\\d\\d([-/.])(0[1-9]|1[012])\2(0[1-9]|[12][0-9]|3[01])\b.+$");
but its still showing 720 rows selected and another line which shouldn’t be there.
Update 2
Working code:
while (readBuff!=null){
Pattern checkData = Pattern.compile("^(19|20)\\d\\d([-/.])(0[1-9]|1[012])\\2(0[1-9]|[12][0-9]|3[01])\\b.+$");
Matcher match = checkData.matcher(readBuff);
if (!match.find()){
readBuff = null;
}
else{
String[] splitReadBuffByComma = new String[3];
splitReadBuffByComma = readBuff.split(",");
for (int x=0; x<splitReadBuffByComma.length; x++){
if (x==0){
dHourList.add(splitReadBuffByComma[x]);
}
else if (x==1){
throughputList.add(splitReadBuffByComma[x]);
}
else if (x==2){
avgRespTimeList.add(splitReadBuffByComma[x]);
}
}
}
readBuff = bf.readLine();
}
I removed else if condition and changed it to else and used the regex suggested by Cylian
now I have the output
2012-11-30 21
2012-11-30 22
2012-11-30 23
Size of date-hour: 720 size of throughput: 720 size of avg resp time: 720
Thanks alot!
Try this [your code, but a bit modified]:
regex anatomy
UPDATE
As long I understand, your input string contains many lines starts with a date but not contains commas in them. For this change the previous Pattern to this following:
or
escaped