Sunday, 8 September 2013

Regular expression for extracting information

Regular expression for extracting information

I have a csv file with the following data format
123,"12.5","0.6","15/9/2012 12:11:19"
These numbers are:
order number
price
discount rate
date and time of sale
I want to extract these data from the line.
I have tried the regular expression:
String line = "123,\"12.5\",\"0.6\",\"15/9/2012 12:11:19\"";
Pattern pattern =
Pattern.compile("(\\W?),\"([\\d\\.\\-]?)\",\"([\\d\\.\\-]?)\",\"([\\W\\-\\:]?)\"");
Scanner scanner = new Scanner(line);
if(scanner.hasNext(pattern)) {
...
}else{
// Alaways goes to here
}
It looks like my pattern is not correct as it always goes to the else
section. What did I do wrong? Can someone suggests a solution for this?
Many thanks.

No comments:

Post a Comment