正则表达式找到2个字符串之间无法正常工作的文本块

时间:2014-06-10 19:10:07

标签: c# regex

我有一个csv文件,里面有多个报告。

报告的布局如下所示。我需要为每个报告提取数据并将其存储在单独的表中。

我有一个适用于regexhero.com的正则表达式,但是当我尝试在c#中实现它时,它不匹配。

任何想法我错了什么?

var reg = new Regex("(\"Report: Midwest Sales\")(.*?)(\"Total Records:)");
var match = reg.Matches(csvReport)
"Report: Midwest Sales"

"Order Number", "Total", "Sales Rep"

"000056","$123","Sally"

"000057","$123","Sally"

"000058,"$123","Sally"

"000059","$123","Sally"

"000060","$312","Roger"

"000061","$312","Roger"

"000062","$312","Roger"

"000063","$312","Roger"

"Total Records: 9"

"Report: Midwest Reps"

"Rep Name", "Rep Id", "Manager"

"Sally","1",""

"Roger","2","1"

"Chris","3","1"

"Gus","4","2"

"Total Records: 4"

1 个答案:

答案 0 :(得分:0)

由于它是一个单独的列表,您应该创建一个与您的不同类型的记录匹配的正则表达式。

((\"Report:([^\"]*"))|(\"Total Records:(.)*\")|([^\n]*))(\n|$)

在匹配组的功能中,您将区分记录。