src/share/classes/java/util/regex/Matcher.java

Print this page

        

@@ -23,10 +23,11 @@
  * questions.
  */
 
 package java.util.regex;
 
+import java.util.Objects;
 
 /**
  * An engine that performs match operations on a {@link java.lang.CharSequence
  * </code>character sequence<code>} by interpreting a {@link Pattern}.
  *

@@ -368,16 +369,41 @@
      *          with the given index
      */
     public int start(int group) {
         if (first < 0)
             throw new IllegalStateException("No match available");
-        if (group > groupCount())
+        if (group < 0 || group > groupCount())
             throw new IndexOutOfBoundsException("No group " + group);
         return groups[group * 2];
     }
 
     /**
+     * Returns the start index of the subsequence captured by the given
+     * <a href="Pattern.html#groupname">named-capturing group</a> during the
+     * previous match operation.
+     *
+     * @param  name
+     *         The name of a named-capturing group in this matcher's pattern
+     *
+     * @return  The index of the first character captured by the group,
+     *          or {@code -1} if the match was successful but the group
+     *          itself did not match anything
+     *
+     * @throws  IllegalStateException
+     *          If no match has yet been attempted,
+     *          or if the previous match operation failed
+     *
+     * @throws  IllegalArgumentException
+     *          If there is no capturing group in the pattern
+     *          with the given name
+     * @since 1.8
+     */
+    public int start(String name) {
+        return groups[getMatchedGroupIndex(name) * 2];
+    }
+
+    /**
      * Returns the offset after the last character matched.  </p>
      *
      * @return  The offset after the last character matched
      *
      * @throws  IllegalStateException

@@ -415,16 +441,40 @@
      *          with the given index
      */
     public int end(int group) {
         if (first < 0)
             throw new IllegalStateException("No match available");
-        if (group > groupCount())
+        if (group < 0 || group > groupCount())
             throw new IndexOutOfBoundsException("No group " + group);
         return groups[group * 2 + 1];
     }
 
     /**
+     * Returns the offset after the last character of the subsequence
+     * captured by the given <a href="Pattern.html#groupname">named-capturing
+     * group</a> during the previous match operation.
+     *
+     * @param  name
+     *         The name of a named-capturing group in this matcher's pattern
+     *
+     * @return  The offset after the last character captured by the group,
+     *          or {@code -1} if the match was successful
+     *          but the group itself did not match anything
+     *
+     * @throws  IllegalStateException
+     *          If no match has yet been attempted,
+     *          or if the previous match operation failed
+     *
+     * @throws  IllegalArgumentException
+     *          If there is no capturing group in the pattern
+     *          with the given name
+     */
+    public int end(String name) {
+        return groups[getMatchedGroupIndex(name) * 2 + 1];
+    }
+
+    /**
      * Returns the input subsequence matched by the previous match.
      *
      * <p> For a matcher <i>m</i> with input sequence <i>s</i>,
      * the expressions <i>m.</i><tt>group()</tt> and
      * <i>s.</i><tt>substring(</tt><i>m.</i><tt>start(),</tt>&nbsp;<i>m.</i><tt>end())</tt>

@@ -516,17 +566,11 @@
      *          If there is no capturing group in the pattern
      *          with the given name
      * @since 1.7
      */
     public String group(String name) {
-        if (name == null)
-            throw new NullPointerException("Null group name");
-        if (first < 0)
-            throw new IllegalStateException("No match found");
-        if (!parentPattern.namedGroups().containsKey(name))
-            throw new IllegalArgumentException("No group with name <" + name + ">");
-        int group = parentPattern.namedGroups().get(name);
+        int group = getMatchedGroupIndex(name);
         if ((groups[group*2] == -1) || (groups[group*2+1] == -1))
             return null;
         return getSubSequence(groups[group * 2], groups[group * 2 + 1]).toString();
     }
 

@@ -1255,6 +1299,19 @@
      */
     char charAt(int i) {
         return text.charAt(i);
     }
 
+    /**
+     * Returns the group index of the matched capturing group.
+     *
+     * @return the index of the named-capturing group
+     */
+    int getMatchedGroupIndex(String name) {
+        Objects.requireNonNull(name, "Group name");
+        if (first < 0)
+            throw new IllegalStateException("No match found");
+        if (!parentPattern.namedGroups().containsKey(name))
+            throw new IllegalArgumentException("No group with name <" + name + ">");
+        return parentPattern.namedGroups().get(name);
+    }
 }