Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 992
Newbie Avatar asked Nov 24 '25 13:11

Newbie


1 Answers

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{};]+)+$
like image 50
Adarsh Avatar answered Nov 26 '25 03:11

Adarsh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!