views:

53

answers:

1

I am trying to create a function that will split a string into search terms. Using this code will work fine:

string TestString = "This is a test";
string[] Terms;
Terms = TestString.Split(" ");

This will split my string into 4 strings: "This", "is", "a", "test". However I want words that are enclosed in quotes to be treated as one word:

string TestString = "This \"test will\" fail";
string[] Terms;
Terms = TestString.Split(" ");

This will split my string into 4 strings, again: "This", "\"test", "will\"", "fail"

What I want is for it split that last string into only 3 strings: "This", "test will", "fail"

Anyone have any idea on how to do this?

+3  A: 

Try using a Regex:

var testString = "This \"test will\" fail";
var termsMatches = Regex.Matches(testString, "(\\w+)|\"([\\w ]+[^ ])\"");
Nicolas Bottarini
For extra credit: get the regex to handle quotes embedded in the quoted portion. Your choice of how to escape the embedded quotes - using `\\`, by doubling up the quotes, or some other mechanism. But single quotes have to be embeddable as well (as do backslashes).
Michael Burr