-1

I have a string like

string str = "[COUNT([Weight] > 10)] < 20 AND [COUNT([Height] < 10)] < 25";

And I want to get the value in square bracket. If I use expression Regex(@"\[.*?\]") => it returns

[COUNT([Weight] and [COUNT([Height]

but I want to get the value

[COUNT([Weight] > 10)] and [COUNT([Height] < 10)]

Could I do that? Please assist me.

Thanks!

3 Answers3

3

How about without regex?

string str = "[COUNT([Weight] > 10] < 20";
var start = str.IndexOf('[');
var end = str.LastIndexOf(']');
Console.WriteLine(str.Substring(start, end - start + 1));

enter image description here

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
1

You may use this regex.

@"\[(?:\[[^\[\]]*\]|[^\[\]])*\]"

DEMO

(?:\[[^\[\]]*\]|[^\[\]])* (Matches [..] block or any char but not of [ or ] ) , zero or more times.

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
0

You can try this regex:

var pattern = @"\[.*\]";

REGEX DEMO

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331