Ronald Wildenberg
Ronald Wildenberg

Reputation: 32104

Regex split string on multiple separators with quotes

I have the following string (the double quotes are part of the string):

"abc def ghi" "%1" "%2"

So the string starts with a double quote, each segment is separated by " " and the string ends with a double quote again. I would like to split this into:

abc def ghi
%1
%2

I tried the following: "(^\")|(\" \")|(\"$)" but that doesn't give me the desired result.

Upvotes: 0

Views: 4142

Answers (3)

ionden
ionden

Reputation: 12776

Regex regex = new Regex("\"(.*?)\"");

Results:

http://rubular.com/r/lXbDIpkRRQ

Upvotes: 1

manojlds
manojlds

Reputation: 301157

Don't complicate it. Just use a string split:

        string test = "\"abc def ghi\" \"%1\" \"%2\"";
        var splits = test.Split(new string[]{"\" \"","\""},StringSplitOptions.RemoveEmptyEntries);
        foreach (var split in splits)
        {
            Console.WriteLine(split);
        }

( removes the superfluous / empty entries as well)

Upvotes: 3

daryal
daryal

Reputation: 14919

string s = "\"abc def ghi\" \"%1\" \"%2\"";
string[] splittedStrings = s.Split('"');
string a = splittedStrings[1];
string b = splittedStrings[3];
string c = splittedStrings[5];

Upvotes: 1

Related Questions