从 CSV 文件读取文本块 - vb.net
我需要解析一个 CSV 文件,其中的文本块根据某些规则以不同的方式处理,例如,
userone,columnone,columntwo
userthirteen,columnone,columntwo
usertwenty,columnone,columntwo
customerone,columnone<br>
customertwo,columntwo<br>
singlevalueone
singlevaluetwo
singlevalueone_otherruleapplies
singlevaluethree_otherruleapplies
每个文本块将被分组,因此前三行将使用某些规则进行解析,依此类推。请注意,最后两组只有一个列,但每组必须以不同的方式处理。
我有机会向客户建议文件的格式,因此我想提出以下建议。
[group 1]
userone,columnone,columntwo
userthirteen,columnone,columntwo
usertwenty,columnone,columntwo
[group N]
rowN
一种类似于几年前的 INI 文件的部分。不过,我想听听您的意见,因为我认为必须有更好的方法来处理这个问题。
我建议使用 XML,但客户更喜欢文本文件。
欢迎任何建议。
m0dest0。
诗。使用 VB.net 和 VS 2008
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用设置为枚举行模式(如果每行具有相同格式)的正则表达式组,或者设置为枚举多行(如果格式不限于单行)。对于多行中的每一行,您可以在模式中包含 \n 以跨多行找到您的模式。如果它在单行上,则不需要在正则表达式匹配模式中包含 \n 也称为回车换行符。
vb.net 以及许多其他现代编程语言都广泛支持分组操作。您可以使用索引组或命名组。
每个名称(例如 header1 或任何您想要命名的名称)都采用以下格式:
有关详细信息,请参阅此链接:如何访问 .NET 正则表达式中的命名捕获组? 。
祝你好运。
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.