title: Extracting segments of a semver version string via Java regex
body: <p>Java 8 here. I am trying to parse a <a href="https://semver.org/" rel="nofollow noreferrer">semver</a> (or at least, my flavor of semver) string and extract out its main segments:</p>
  
  <ul>
  <li>Major version number</li>
  <li>Minor version number</li>
  <li>Patch number</li>
  <li>Qualifier (<code>RC</code>, <code>SNAPSHOT</code>, <code>RELEASE</code>, etc.)</li>
  </ul>
  
  <p>Here is my code:</p>
  
  <pre class="lang-java prettyprint-override"><code>String version = "1.0.1-RC";
  Pattern versionPattern = Pattern.compile("^[1-9]\\d*\\.\\d+\\.\\d+(?:-[a-zA-Z0-9]+)?$");
  Matcher matcher = versionPattern.matcher(version);
  
  if (matcher.matches()) {
  
      System.out.println("\n\n\matching version is: " + matcher.group(0));
      System.out.println("\nmajor #: " + matcher.group(1));
      System.out.println("\nminor #: " + matcher.group(2));
      System.out.println("\npatch #: " + matcher.group(3));
      System.out.println("\nqualifier: " + matcher.group(4) + "\n\n\n");
  
  }
  </code></pre>
  
  <p>When this runs, I get the following output on the console:</p>
  
  <pre><code>matching version is: 1.0.1-RC
  2019-10-18 14:32:05,952 [main] 84b37cef-70f9-4ab8-bafb-005821699766 ERROR c.s.f.s.listeners.StartupListener - java.lang.IndexOutOfBoundsException: No group 1
  </code></pre>
  
  <p>What do I need to do to my regex and/our use of the Matcher API so that I can extract:</p>
  
  <ul>
  <li><code>1</code> as the major number</li>
  <li><code>0</code> as the minor number</li>
  <li><code>1</code> as the patch number</li>
  <li><code>RC</code> as the qualifier</li>
  </ul>
  
  <p>Any ideas?</p>