Regular expression to read a record in a text(CSV) file

Hi

while parsing the CSV file i need to validate each row or record that it should not contain any special characters outside the double Quotes and the no of fields should be 13.

some of the records that i have corrupted from the CSV file are as follows

"0323034055","000","2823484","2005/10/17","S","016184","CAROLE CROW","COLUMBIA","SC","29203"," ","1","N"

"0323034055","0,0,0,","2823484","2005/10/17","S","016184","CAROLE CROW","COLUMBIA","SC","29203"," ","1","N"

"0323034055","000"@,"2823484","2005/10/17","S","016184","CAROLE CROW","COLUMBIA","SC","29203"," ","1","N"

"0323034055","000","2823484","2005/10/17","S","016184","CAROLE CROW","COLUMBIA","SC","29203"," "

"0323034055","000","2823484","2005/10/17","S","016184","CAROLE CROW","COLUMBIA","SC","29203"," ","1","N”, “”

the 1st, 2nd, 3rd, 6th, 10th, 12th fields should be number , 4th field should be date, 5th and 13th a char,11th and 7th a alphanumeric string

We can have an empty string also for thse fields.

Please help me in this

Thanks

Regards,
Kiran




Answer this question

Regular expression to read a record in a text(CSV) file

  • Stelios

    This is a perfect file for the FileHelpers library:

      http://filehelpers.sourceforge.net

      you need to define a class for the records:

    [DelimitedRecord("\"")]
    public YourClass
    {
       [FiledQuoted()]
       public int First;
       [FiledQuoted()]
       public int Second..

       [FiledConverter(ConvertKind.Date, "yyyy/MM/dd")]
       public DateTime Fourth
      ....
    }

     and then use the library like this.

      FIleHelpersEngine engine = new FileHelperEngine(typeof(YourClass));

       // to read
       YourClass[] res = engine.ReadFile("csv.txt");

       // to write
       engine.WriteFile("csvout.txt")

    Best Regards


  • ayat108

    thanks for the sugggestion...i was able to solve the Query with the following code

    if(System.Text.RegularExpressions.Regex.IsMatch(strFileRecord,"^\"[0-9Xx]{10}\",\".{0,3}\",\".{0,12}\",\"[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}\",\"(S|R)\",\".{0,19}\",\".{0,50}\",\"[a-zA-Z -.]{0,25}\",\"[a-zA-Z ]{0,2}\",\".{0,10}\",\"[a-zA-Z ]{0,3}\",\"[-0-9]{0,6}\",\"(N|Y)\"$", RegexOptions.Singleline))
    return true;
    else
    return false;

    i was reading from a CSV file and strFileRecord has a complete record from the CSV file..

    Kiran



  • Rune Gulbrandsen

    You would probably have to go with the Regex.Split() method that will split a line into parts. The code would look something like this:

    string input = textBox1.Text;

    if (input.IndexOf('"') == 0)

    {

    input = input.Substring(1);

    }

    if (input.LastIndexOf('"') == input.Length - 1)

    {

    input = input.Substring(0, input.Length - 1);

    }

    string[] parts = Regex.Split(input, @"\"",\s \""");

    Then you can check the number of elements of parts and validate each element accordingly.

    Note: If you are into design patterns at all, Object Builder may be applied in this case.

    hth


  • Regular expression to read a record in a text(CSV) file