I would like to return a array string[] or a list List<string> using regex.
I want to compare a string and return all values that starts with [ and end with ].
I am new to regex, so would you please explain the syntax that you will be using to produce the right results.
var result = Regex.Matches(input, @"\[([^\[\]]*)\]")
.Cast<Match>()
.Select(m => m.Groups[1].Value).ToArray();
This regex \[([^\[\]]*)\] means:
\[ - character [([^\[\]]*) - any character, excluding [] any number of repetitions, group 1\] - character ]Update:
var result = Regex.Matches(input, @"\[[^\[\]]*\]")
.Cast<Match>()
.Select(m => m.Value).ToArray();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With