Search code examples
c#regexstringcharacter

How to find groups in text with a separator


I have the following text

"a|mother" "b|father"

I want to find via Regex, groups of text that starts with '"' and ends with '"' and separate with '|' without spaces. Meaning the results would be:

  1. "a|mother"
  2. "b|father"

How can I find the |? and how can I find my pattern without spaces?


Solution

  • Something like this:

      String source = "\"a|mother\" \"b|father\"";
    
      var result = Regex
        .Matches(source, "\"[^\"]*[^ ]\\|[^ ][^\"]*\"")
        .OfType<Match>();
    
      Console.Write(String.Join(Environment.NewLine, result));
    

    Output is

    "a|mother"
    "b|father"