Orsi
Orsi

Reputation: 575

Regex for balanced and unbalanced strings

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.

Upvotes: 1

Views: 348

Answers (0)

Related Questions