1

I'm using the followin RegEx which I found in this similar question: /[^\s"]+|"[^"]*"/g

I want to split the string by spaces except when strings are quoted by double quotes. The quotes have to be included by the result.

This is my C# code:

MatchCollection conditions = Regex.Matches(strCondition, @"/[^\s""]+|""[^""]*""/g"); 

conditions.Count is always zero! When I test the regex online, it works!

Example input:

"0" MATCHES NOCASE "Den Bosch"  OR "0" MATCHES NOCASE "'s Hertogenbosch"

"0" MATCHES "1" AND "1" MATCHES "2" AND "2" MATCHES "3" AND "3" MATCHES "4" AND "4" MATCHES "5"

What am I doing wrong? I wasted several hours and have no clue anymore.

This one: (?<=")\w[\w\s]*(?=")|\w+|"[\w\s]*" does give results in my case, but does not give the desired result because it fails on single quotes.

greg-449
  • 109,219
  • 232
  • 102
  • 145
Roel
  • 3,089
  • 2
  • 30
  • 34

1 Answers1

4

Regular expressions in .NET don't use the / delimiters, so:

MatchCollection conditions = Regex.Matches(strCondition, @"[^\s""]+|""[^""]*"""); 
Sean Bright
  • 118,630
  • 17
  • 138
  • 146
  • Fantastic! I knew it had to be something small. Thank you very much now I think I can finish it before the deadline. – Roel Sep 18 '12 at 13:13