Home > Back-end >  Split string into groups by inside and outside of brackets - including the brackets
Split string into groups by inside and outside of brackets - including the brackets

Time:08-11

I'm looking for the simplest way to parse a string into groups - including the delimiters. What's inside and outside the { } could be anything - except for { and }.

Using this

var s = "test {#}, {1} more text {a|b|c}";  
var regEx = new Regex(@"{(.*?)}");  
var mat = regEx.Matches(s);

I get groups that are ["{#}", "{1}", "{a|b|c}"].

My desired result is ["test ", "{#}", ", ", "{1}", " more text ", "{a|b|c}"].

CodePudding user response:

If inside and outside can be anything except for { and }:

\{[^{}]*}|[^{}] 

Explanation

  • { Match the opening {
  • [^{}]* Optionally match any char except { and }
  • } Match the closing }
  • | Or
  • [^{}] match 1 chars other than { and }

Regex demo

Example

string pattern = @"{[^{}]*}|[^{}] ";
string input = @"test {#}, {1} more text {a|b|c}";
foreach (Match m in Regex.Matches(input, pattern))
{
    Console.WriteLine("'{0}'", m.Value);
}

Output

'test '
'{#}'
', '
'{1}'
' more text '
'{a|b|c}'

CodePudding user response:

The Regex.Split method can find the bracketed text as separators for the split. Exclosing the separator in capture brackets means that both the separators and the other text is saved. The following code gives what is required.

        var s = "test {#}, {1} more text {a|b|c}";
        Regex regEx = new Regex(@"(\{.*?\})");
        string[] substrings = regEx.Split(s);

        foreach (string match in substrings)
        {
            Console.WriteLine("'{0}'", match);
        }
  • Related