I have a string like :
"abc{d}efg{hi}{jk}lm{n}"
And I want it to be split into:
"abc","{d}","efg","{hi}","{jk}","lm","{n}"
I used this pattern [{}] and the result is "abc","d","efg","hi","","jk","lm","n"
How do I keep the '{'and'}' there? And how do I remove the empty "" between '}'and'{'?
Remember that Match All and Split are Two Sides of the Same Coin.
Use this regex:
{[^}]*}|[^{}]+
See the matches in the DEMO.
To see the matches:
var myRegex = new Regex("{[^}]*}|[^{}]+");
Match matchResult = myRegex.Match(yourString);
while (matchResult.Success) {
Console.WriteLine(matchResult.Value);
matchResult = matchResult.NextMatch();
}
Explanation
|, the {[^}]*} matches {content in braces}
[^{}]+ matches any chars that are not curliesIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With