0

Here's my code:

public void DeserialStream(string filePath)
    {
        using (StreamReader sr = new StreamReader(filePath))
        {
            string currentline;
            Regex countRegex = new Regex("\"DataType\",\"(?:Count|Net)\"((?!\"DataType\").)*", RegexOptions.Singleline); 
            while ((currentline = sr.ReadLine()) != null)
            {
                    foreach (Match match in countRegex.Matches(currentline))
                    {
                        Console.WriteLine(match.Value); 
                    }
                }

            }
        }

Here's my CSV:

"Date","dd/mm/yyyy"
"ExpirationDate","dd/mm/yyyy"

"DataType","Count"
"Location","Unknown","Variable1","Variable2","Variable3"
"A(Loc3, Loc4)","Unknown","5656","787","42"
"A(Loc5, Loc6)","Unknown","25","878","921"

"DataType","Net"
"Location","Unknown","Variable1","Variable2","Variable3"
"A(Loc3, Loc4)","Unknown","5656","787","42"
"A(Loc5, Loc6)","Unknown","25","878","921"

I want to return the data between "DataType", "Count" and "DataType", "Net" below. However, my compiler is returning the terms: "DataType", "Count" and "DataType", "Net" and not the data in between. I'm not sure what I am doing wrong?

Kala J
  • 2,040
  • 4
  • 45
  • 85
  • possible duplicate of [How can I grab comma delimited values that appear after a term I searched for?](http://stackoverflow.com/questions/23763992/how-can-i-grab-comma-delimited-values-that-appear-after-a-term-i-searched-for) – Sam May 20 '14 at 18:28
  • Are you trying to match the header rows, or the actual data rows below them? Your Regex, I believe, will match the header row, not the data rows... – LB2 May 20 '14 at 18:48
  • I am trying to match the actual data rows below Count but above Net. Not the headers... – Kala J May 20 '14 at 18:53

3 Answers3

0

You need to remove the : from DataType:

Edit: Try this Regex "\"DataType\",\"(?:Count|Net)\"((?:(?!\"DataType\").)*)"

Farhad Alizadeh Noori
  • 2,276
  • 17
  • 22
0

This may be what you're looking for, though you may need to adjust:

\"DataType\",\"(?:Count|Net)\"\n((((?<Location>\"[^\"]*\"),(?<Unknown>\"[^\"]*\"),(?<Var1>\"[^\"]*\"),(?<Var2>\"[^\"]*\"),(?<Var3>\"[^\"]*\"))\n)*)
  • Keep in mind that it doesn't support correct CSV parsing (will not handle escaped " in the middle of value).
  • Keep in mind that it doesn't allow for spaces between commas
  • It assumes there is a blank line after data rows and before next header rows.
  • Whatever else may be left out :) - adjust accordingly (i.e. it may have extra capture groups, so you may need to adjust for that). This is intended more to get you going than a perfect solution.
LB2
  • 4,802
  • 19
  • 35
  • I have a question... since it Regex seems more painful than initially though, is there a way to grab that data I want besides RegEx? – Kala J May 20 '14 at 19:28
  • Yes, by using any suitable CSV parser. In fact, that would be a preferred way. You can find SO post on it at http://stackoverflow.com/questions/9642055/csv-parsing-options-with-net – LB2 May 20 '14 at 20:50
0

I think this pattern will do \"DataType\",\"Count\"(.*?)\"DataType\",\"Net\" w/ s option
Demo

alpha bravo
  • 7,838
  • 1
  • 19
  • 23
  • Doesn't seem to work on Rubular, http://rubular.com/. Not sure why... How do I include s modifier in the regular expression above? – Kala J May 21 '14 at 10:02
  • 1
    use this instead `"DataType","Count"([\s\S]*?)"DataType","Net"` [rubular Demo](http://rubular.com/r/KiaKYTbxeA) – alpha bravo May 22 '14 at 04:53