Trimack
Trimack

Reputation: 4233

Regex match a CSV file

I am trying to create a regex to match a CSV file of records in the form of:

optional value, , ,, again some value; this is already, next record;

Now there is an upper limit of commas (10) separating attributes of each record and unlimited number of ; separating each record. Values might or might not be present. I am unexperienced with regex and my effort is rather futile so far. Please help. If necessary, I will include more details.

EDIT

I want to verify that the file is in the required form and get the number of records in it.

Upvotes: 1

Views: 1103

Answers (3)

VVS
VVS

Reputation: 19612

Reminds me to the famous article form Coding Horror: Regular Expressions: Now You Have Two Problems.

FileHelpers saved my day when dealing with CSV or other text format.

Upvotes: 0

Dean Chalk
Dean Chalk

Reputation: 20481

try this

bool isvalid = csv.Split(';')
                    .Select(c => c.Split(',')
                        .Count())
                    .Distinct()
                    .Count() == 1;

Upvotes: 1

Mario
Mario

Reputation: 36567

Do you really need to use regular expressions for this? Might be a little bit overkill. I'd just perform one String.Split() to get the records, then another String.Split() on each record to get the values. Also rather easy to get the number of elements etc. then.

If you really want to use Regexps, I'd use two steps again: /(.*?);/ to get the datasets; /(.*?)[,;]/ to get the values.

Could probably be done with one regexp as well but I'd consider this overkill (as you'd have to find the sub matches etc. identify their parent record, etc.).

Escaped characters would be another thing but rather similar to do: e.g. /(.*?[^\\]);/

Upvotes: 1

Related Questions