Get all captured groups in Java
<p>I want to match a single word inside brackets(including the brackets), my <code>Regex</code> below is working but it's not returning me all groups.</p>

<p>Here's my <strong>code:</strong></p>

<pre><code>String text = "This_is_a_[sample]_text[notworking]";
Matcher matcher = Pattern.compile("\\[([a-zA-Z_]+)\\]").matcher(text);                                     
if (matcher.find()) {
    for (int i = 0; i &lt;= matcher.groupCount(); i++) {
    System.out.println("------------------------------------");
    System.out.println("Group " + i + ": " + matcher.group(i));
}
</code></pre>

<p>Also I've tested it in <a href="http://www.regexplanet.com/cookbook/ahJzfnJlZ2V4cGxhbmV0LWhyZHNyDwsSBlJlY2lwZRjqst8hDA/index.html" rel="noreferrer">Regex Planet</a> and it seems to work.</p>

<p>It must return 4 groups:</p>

<pre><code>------------------------------------
Group 0: [sample]
------------------------------------
Group 1: sample
------------------------------------
Group 2: [notworking]
------------------------------------
Group 3: notworking
</code></pre>

<p>But it's returning just it:</p>

<pre><code>------------------------------------
Group 0: [sample]
------------------------------------
Group 1: sample
</code></pre>

<p>What's wrong?</p>