3

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?

6
  • The given CSV should match for all the lines ? Commented Nov 21, 2013 at 7:22
  • Yes but is there a way to convert the csv to a xml and validate?> Commented Nov 21, 2013 at 7:54
  • The regex in my answer does the job. Commented Nov 21, 2013 at 8:04
  • Why do u want to convert it into an xml ? Commented Nov 21, 2013 at 8:05
  • So that it would be easy to validate an xml Commented Nov 21, 2013 at 9:36

3 Answers 3

2

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{};]+)+$
Sign up to request clarification or add additional context in comments.

Comments

1

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

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.