1

I have a target string like this: (however nesting can be very deep in practice)

{hi {how {are {you}}}}

Desired result would be:

Groups: hi how are you

I can't find anything in c# regex to do nested capturing like that. Is it possible at all?

EDIT:

I think I simplified my example too much which obscures the answers. I need to capture in a recursive sort of way because I need the content inside the brackets: {test[{test2[content]}]}

where the desired result would be:

{test2[content]} and content

Blub
  • 13,014
  • 18
  • 75
  • 102

2 Answers2

0
{([^{}]+)

Try this.See demo.grab the captures.

http://regex101.com/r/oE6jJ1/35

vks
  • 67,027
  • 10
  • 91
  • 124
  • 1
    Sorry, I was asking for a general way on how to capture in a nested fashion. Your solution happens to work on this example, but it won't for pretty much any non trivial recursive capturing problem. – Blub Nov 26 '14 at 14:42
0

I could think of this:

 string balh = "{hi {how {are {you}}}}";
 string[] foo = balh.Split(new char[] { '{', '}' }, StringSplitOptions.RemoveEmptyEntries);
 string output = string.Join(" ", foo);

The groups will be in foo array.

EDIT:

I think you are looking for more elaborate inputs. I got the question wrong :(

Piyush Parashar
  • 866
  • 8
  • 20