I want to just extract the text between the brackets — NOT the brackets, too!
My code looks currently like this:
var source = "Harley, J. Jesse Dead Game (2009) [Guard]"
// Extract role with regex
m = Regex.Match(source, @"\[(.*)\]");
var role = m.Groups[0].Value;
// role is now "[Guard]"
role = role.Substring(1, role.Length-2);
// role is now "Guard"
Can you help me to simplify this to just a single regex, instead of the regex, then the substring?
you use a different group number. Every time you wrap something in ( ) it creates a new group out of it. Group zero is the entire found expression. group1 is the first group of (), group2 is the second, etc. Since you’re using group 0, it’s returning the entire string that matches the expression
Try changing Groups[x] to 1 and see what it gives you.