0

What does this regular expression do? -

Regex.Match(file.ToString(), @"\(([^)]*)\)").Groups[1].Value.ToString();

( = starting paranthesis

) = ending paranthesis

[^)]* = all characters which are not ending paranthesis

What is the need for another () wrapping around the square brackets?

vijayst
  • 14,909
  • 15
  • 55
  • 99
  • You should take a look to [regex101](https://regex101.com/) which provides tests and explanation. – Delgan Jun 25 '15 at 07:17
  • @Delgan: Although sadly not for .Net's flavor of regex. But the flavors it has are quite similar (and the above is the same in several flavors). – T.J. Crowder Jun 25 '15 at 07:46

1 Answers1

1

What is the need for another () wrapping around the square brackets?

That defines a capture group for the zero or more characters matched by [^)]*, so that you can get only the text that matched that part, without the ( and ) literal characters around it. E.g., given the input (foo), the overall match is (foo) but the capture group contains foo.

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639