Search code examples
regexpattern-matchingregular-language

Regular expression validate CSV


I want to check if a CSV file is valid using Regex but I have a problem.

Here is the example I want to match :

[start]13 numbers;anything not containing ";"[nothing after this]

For example :
match :

1234567891234;some text

doesn't match because of the second ";" :

1234567891234;some text;

The problem is when there is something after the second ";".
This one will match but I don't know how to avoid it:

1234567891234;some text; sometext

Here is the regex I use and an example :

^(\d{13});(.*(?<!\;)$)

Solution

  • It seems you tried [^;]* but since this negated character class matches line breaks, it did not work for you. All you need is to add LF / CR into the class:

    ^(\d{13});([^;\r\n]*)$
    

    See the regex demo.

    Details:

    • ^ - start of string
    • (\d{13}) - Group 1: thirteen digits
    • ; - a semi-colon
    • ([^;\r\n]*) - Group 2: zero or more chars other than CR, LF and ;
    • $ - end of string.