Newbie
Newbie

Reputation: 2708

Regex to validate CSV in JAVA

Hi I am trying to validate a CSV sheet line by line . The csv file contains :-

9,EditTest,expectedResult=nooptionsacltrue
10,AddTest,features={w;f;r}
1,AddTest,projectType=new,vtName=28HPM,status=ACTIVE,canOrder=Yes,expectedResult=duplicate
2,AddTest,projectType=old,vtName=28nm,status=ACTIVE,canOrder=Yes,expectedResult=invalidfeatures

Here is my code :-

    public class Example {

    public static void main(String[] args) throws Exception {

            BufferedReader br = new BufferedReader(new FileReader("testdbcsv/ACL.csv"));
            while((line=br.readLine())!=null){
                String pattern = "^(\\d+),(\\w+),((\\w+)=(\\w+)),((\\w+)=(\\w+)+?";
                if(line.matches(pattern)){
                    System.out.println("pattern matched");
                }
//Code
}

But my regex seems incorrect . Can anyone please help me with the correct regex for the csv. Thanks . Or is there any other way where I can validate my CSV according to a schema other than using regular expressions?

Upvotes: 3

Views: 9612

Answers (3)

AJ.
AJ.

Reputation: 4534

try this one "\\d+,\\w+(,\\w+=\\w+)+"

Upvotes: 1

Adarsh
Adarsh

Reputation: 3641

Assuming that the validation for the second line should fail,

^(\\d),[A-Za-z]+(,[A-Za-z]+=[A-Za-z0-9]+)+$

EDIT

This should match all four lines in your given csv.

^(\\d)+,[A-Za-z]+(,[A-Za-z]+=[A-Za-z0-9{};]+)+$

Upvotes: 2

OneSix
OneSix

Reputation: 71

CsvValidator, A Java framework which validates any CSV files can used.

Check this url : http://mdshannan1.blogspot.in/2011/07/validating-csv-files.html

Upvotes: 1

Related Questions