m0dest0
m0dest0

Reputation: 859

Reading blocks of text from a CSV file - vb.net

I need to parse a CSV file with blocks of text being processed in different ways according to certain rules, e.g.

userone,columnone,columntwo
userthirteen,columnone,columntwo
usertwenty,columnone,columntwo

customerone,columnone<br>
customertwo,columntwo<br>

singlevalueone
singlevaluetwo

singlevalueone_otherruleapplies
singlevaluethree_otherruleapplies

Each block of text will be grouped so the first three rows will be parsed using certain rules and so on. Notice that the last two groups have only one single column but each group must be handled in a different way.

I have the chance to propose the customer the format of the file so I'm thinking to propose the following.

[group 1]
userone,columnone,columntwo
userthirteen,columnone,columntwo
usertwenty,columnone,columntwo

[group N]
rowN

A kind of sections like the INI files from some years ago. However I'd like to hear your comments because I think there must be a better way to handle this.

I proposed to use XML but the customer prefers the text files.

Any suggestions are welcome.

m0dest0.

Ps. using VB.net and VS 2008

Upvotes: 1

Views: 429

Answers (1)

RetroCoder
RetroCoder

Reputation: 2685

You can use regular expression groups set to either an enum line mode if each line has the same format, or to an enum multi-line if the format is not constrained to a single line. For each line in multiline you can include \n in your pattern to cross multiple lines to find you pattern. If its on a single line you don't need to include \n also know as Carriage return line feed in your regex matching pattern.

vb.net as well as many other modern programming language has extensive support for grouping operations. You can use index groups, or named groups.

Each name such as header1 or whatever you want to name it would be in this format: <myname>
See this link for more info: How do I access named capturing groups in a .NET Regex?.

Good luck.

Upvotes: 1

Related Questions