-5

I have a string like this [test]][test][test]

I would like with a regex to obtain a collection of elements where each element will be a value between the brackets [] :

test
test
test

With this code:

var pattern = @"\[(.*?)\]";            
var results = Regex.Matches("[test]][test][test]", pattern);

I managed to get the values but they include the brackets [] :

[test]
[test]
[test]

user2443476
  • 1,935
  • 9
  • 37
  • 66
  • You just need to get the values from the first captured group `$1` – Tushar Feb 18 '16 at 14:55
  • Also see [How do I access named capturing groups in a .NET Regex?](http://stackoverflow.com/questions/906493/how-do-i-access-named-capturing-groups-in-a-net-regex) – Tushar Feb 18 '16 at 14:58
  • did my answer help you solving your problem? – fubo Feb 22 '16 at 06:45

1 Answers1

1

This should works for you:

var pattern = @"\[(?<elem>.*?)\]";
var results = Regex.Matches("[test1]][test2][test3]", pattern);

foreach (Match item in results)
    Console.WriteLine(item.Groups["elem"]);
Arturo Menchaca
  • 15,783
  • 1
  • 29
  • 53