-2

I'm currently trying to split a string into its pieces using this:

var output = Regex
                .Split(input, @"(?<=[)])\s*|\s*(?=[(])")
                .Where(s => s != string.Empty)
                .ToList();

The input string is: "hmmmmmmmm (red,asdfhqwe) asasd"

The wanted output is: "hmmmmmmmm ", "(red,asdfhqwe)", " asasd".

the output I'm getting is: "hmmmmmmmm", "(red,asdfhqwe)" and "asasd".

How can i include the spaces when splitting?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563

1 Answers1

0

The current (?<=[)])\s*|\s*(?=[(]) regex matches 0+ whitespaces with \s*, and that is why they are missing.

You might just remove \s* from the regex, and (?<=[)])|(?=[(]) should already work in most cases.

However, you may use other approaches where you can control what (...) substrings you split out.

For example, you may use (\([^()]*\)) regex:

var output = Regex
            .Split(input, @"(\([^()]*\))")
            .Where(s => !string.IsNullOrEmpty(s))
            .ToList();

It will match and capture substrings inside parentheses and thus the matches will also be part of the resulting list.

See the online C# demo and the online regex demo.

Split list:

enter image description here

NOTE: To split out substrings between balanced parentheses, use

@"(\((?>[^()]+|(?<c>)\(|(?<-c>)\))*\)(?(c)(?!)))"

See another C# demo. See this answer for this regex description. More on this can be found at the regular-expressions.info Balancing Groups.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563