Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Regular expression for csv validation...

Last post 04-04-2008, 4:59 PM by Lyndar. 1 replies.
Sort Posts: Previous Next
  •  04-04-2008, 6:56 AM 41067

    Regular expression for csv validation...

    Hiya all,

    I asked this last week but I never gave a proper real live example of my csv.....Sorry ..My Fault

    Here goes...I need a regular expression to validate a csv for the below file example.

    0067,Medium,7NOK6XX45,,,CHEMDRY SECTION,5 The River,Riverside Lock,Kilcullen,Co Kildare,028BS007REIL3000,Donn,McGull,045481400

    0067,Medium,7NOK62245,,,WALLACE SECTION,5 The Ware,Moore Side,Drumgan,Co Clare,028BS007REIL9000,Donn,McGull,045481422

    Validation Needed is

    Total Number of fields must be 14

    Field Lengths can not exceed Spec (Below)

    Field3 and Field 11 must be CAPS

    Field1 and field3 and field6 and field7 and field11 can not be empty.

    Field length Chart:

    FieldNumber MaxLength
    Field1               9
    Field2               10
    Field3               15
    Field4               19
    Field5               10
    Field6               36
    Field7               36
    Field8               36
    Field9               36
    Field10             36
    Field11             16
    Field12             36
    Field13             36
    Field14             16

    Can someone please help...

    Ray..

  •  04-04-2008, 4:59 PM 41099 in reply to 41067

    Re: Regular expression for csv validation...

    First, take a look at the FileIO.TextFieldParser class.  It was designed for reading in text files in fixed-length or delimited format.

    If you decide to stick with a regex implementation, you can do something like the following:

    \d{1,9},\w{0,10}, etc

    Basically, you first look at each field and decide what characters are valid in it.  Then you put {minimum,maximum} after this to specify the allowed size of the field.  Then you put a comma.  Repeat until the end.

    Common character-based questions are:

    1) Can the field hold numbers?

    2) Can the field hold characters?

    3) Can the field hold quotation marks, possibly enclosing commas?

     

    Etc.

    Or, you can use the TextFieldParser class and a simple for loop to evaluate the populate fields.  Up to you.

View as RSS news feed in XML