1

I need to match strings that can be between parentheses {}. The string can also contain more types of parentheses. It can be balanced and unbalanced, too.

For example: {Title} {Size{()} {Color({[]})} {Test{} {Color{}}

So, I need to remove actually the strings between the {....}

In this case the matched strings should be: Title, Size{(), Color({[]}), Test{, Color{}

I'm using this regex

Regex regex =  new Regex("\\{([^}]+)}");

I'm using then:

regex.Matches(checkedString), where checkedString could be: ( {Title} {Size{()} {Color({[]})} {Test{}  {Color{}})

It works only when the parenthesis is not closed, so it works well for: {Size{()} - it gets the Size{() string. it does not work well for {Color{}} , so where the parenthesis are balanced. In this case I should get Color{}

Any help is appreciated.

mbomb007
  • 3,788
  • 3
  • 39
  • 68
Orsi
  • 545
  • 2
  • 10
  • 27
  • 3
    Looks like ,NET, try `var results = Regex.Matches(text, @"(?<!\S)\{(\S*?)}(?!\S)").Cast().Select(x => x.Groups[1].Value).ToList();`. I assume all your matches are between whitespaces or start/end of string and have no whitespace in them. – Wiktor Stribiżew Dec 21 '20 at 21:06
  • It'd help if you had better definition of what is "valid" for you. For example - ares paces meaningful (`{Test{}{Color{}}`, `{ Color( {[] } )}`)? Where can words be (`{Size{If(x)M}`)? How many levels of braces can you have (`{Color{{Color{}}}`)? In general, it is difficult to match non-balanced string in a way that isn't ambiguous, so it's easier if the rules are cleared, or if this format has a spec or a name (looks like a templating language?). – Kobi Dec 22 '20 at 05:29
  • @WiktorStribiżew- thanks, it works perfectly – Orsi Dec 22 '20 at 07:28

0 Answers0