title: How to get the names of the regex named capturing group in a match in Java?
body: <p>Given:</p>
  
  <pre><code>String text = "FACEBOOK is buying GOOGLE and FACE BOOK";
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>Pattern pattern = Pattern.compile("(?&lt;FB&gt;(FACE(\\p{Space}?)BOOK))|(?&lt;GOOGL&gt;(GOOGL(E)?))");
  Matcher matcher = pattern.matcher(text);
  </code></pre>
  
  <p>I want to get something like this:</p>
  
  <pre><code>Group=FB matches substring="FACEBOOK" at position=[0, 8)
  Group=GOOGL matches substring="GOOGLE" at position=[19, 25)
  Group=FB matches substring="FACE BOOK" at position=[30, 39)
  </code></pre>
  
  <p>However, I have been unable to get the group name. Here is my attempt in Scala:</p>
  
  <pre><code>import java.util.regex.Pattern
    val pattern = Pattern.compile("(?&lt;FB&gt;(FACE(\\p{Space}?)BOOK))|(?&lt;GOOGL&gt;(GOOGL(E)?))")
    val text = "FACEBOOK is buying GOOGLE and FACE BOOK"
    val matcher = pattern.matcher(text)
  
    while(matcher.find()) {
      println(s"Group=???? matches substring=${matcher.group()} at position=[${matcher.start},${matcher.end})")
    }
  </code></pre>
  
  <p>EDIT:
  Someone marked this as a duplicate of <a href="https://stackoverflow.com/questions/15588903/get-group-names-in-java-regex">Get group names in java regex</a> but this is a different question. This is asking given a MATCH, how to find the group name. The other question is asking how to get the group-name to String (or index) given a Pattern object.</p>