title: Regular Expression Groups in C#
body: <p>I've inherited a code block that contains the following regex and I'm trying to understand how it's getting its results.</p>
  
  <pre><code>var pattern = @"\[(.*?)\]";
  var matches = Regex.Matches(user, pattern);
  if (matches.Count &gt; 0 &amp;&amp; matches[0].Groups.Count &gt; 1)
      ...
  </code></pre>
  
  <p>For the input <code>user == "Josh Smith [jsmith]"</code>:</p>
  
  <pre><code>matches.Count == 1
  matches[0].Value == "[jsmith]"
  </code></pre>
  
  <p>... which I understand. But then:</p>
  
  <pre><code>matches[0].Groups.Count == 2
  matches[0].Groups[0].Value == "[jsmith]"
  matches[0].Groups[1].Value == "jsmith" &lt;=== how?
  </code></pre>
  
  <p>Looking at <a href="https://stackoverflow.com/questions/3320823/whats-the-difference-between-groups-and-captures-in-net-regular-expressions">this question</a> from what I understand the Groups collection stores the entire match as well as the previous match. But, doesn't the regexp above match only for [open square bracket] [text] [close square bracket] so why would "jsmith" match?</p>
  
  <p>Also, is it always the case the the groups collection will store exactly 2 groups: the entire match and the last match?</p>