extract double quoted items in a phrase

c#, regex, split, string

Solution

I think you need something like `"(?<word>[^"]+)"|(?<word>\w+)`. This will match both text in double quotes and single words:

var str = @"SO ""sales manager"" marketing hello ""management""";
var regex = new Regex(@"""(?<word>[^""]+)""|(?<word>\w+)");
var words = regex.Matches(str)
    .Cast<Match>()
    .Select(m => m.Groups["word"].Value)
    .ToArray();

For the test string this will return:

SO
sales manager
marketing
hello
management

Problem

I would like to extract all double quoted phrase inside an input phrase and keep non matching elements as words let's say i have "sales people" IT i want the output to be: ``` sales people IT ``` same thing for input="SO \"sales manager\" marketing \"management\"" the output is: ``` SO sales manager marketing management ``` if input ="SO \"sales manager\" marketing management\" insurance" the output is: ``` SO sales manager marketing management insurance ``` I have found the regex :but i don't know how to extract: ``` string InputText="SO \"sales manager\" marketing \"management\"" ; string pattern0 = "^\"(.*?)\"$"; string pattern = "^(.*?)\"(.*?)\"(.*?)$"; Regex regex = new Regex(pattern); string[] temOperands; bool isMatch = regex.IsMatch(InputText); if (isMatch) { //here goes the extraction } ```

Original source