--- old/src/share/classes/java/util/regex/Matcher.java 2013-04-30 13:16:55.000000000 -0700 +++ new/src/share/classes/java/util/regex/Matcher.java 2013-04-30 13:16:55.000000000 -0700 @@ -25,6 +25,7 @@ package java.util.regex; +import java.util.Objects; /** * An engine that performs match operations on a {@link java.lang.CharSequence @@ -370,12 +371,37 @@ 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 + * named-capturing group 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.
* * @return The offset after the last character matched @@ -417,12 +443,36 @@ 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 named-capturing + * group 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. * * For a matcher m with input sequence s,
@@ -518,13 +568,7 @@
* @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();
@@ -1257,4 +1301,17 @@
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);
+ }
}
--- old/test/java/util/regex/RegExTest.java 2013-04-30 13:16:57.000000000 -0700
+++ new/test/java/util/regex/RegExTest.java 2013-04-30 13:16:56.000000000 -0700
@@ -33,7 +33,7 @@
* 5013885 5003322 4988891 5098443 5110268 6173522 4829857 5027748 6376940
* 6358731 6178785 6284152 6231989 6497148 6486934 6233084 6504326 6635133
* 6350801 6676425 6878475 6919132 6931676 6948903 6990617 7014645 7039066
- * 7067045 7014640 7189363 8007395
+ * 7067045 7014640 7189363 8007395 8013252 8013254
*/
import java.util.regex.*;
@@ -3390,7 +3390,9 @@
private static void check(Pattern p, String s, String g, String expected) {
Matcher m = p.matcher(s);
m.find();
- if (!m.group(g).equals(expected))
+ if (!m.group(g).equals(expected) ||
+ s.charAt(m.start(g)) != expected.charAt(0) ||
+ s.charAt(m.end(g) - 1) != expected.charAt(expected.length() - 1))
failCount++;
}
@@ -3420,19 +3422,42 @@
failCount++;
}
- private static void checkExpectedFail(Matcher m, String g) {
+ private static void checkExpectedIAE(Matcher m, String g) {
m.find();
try {
m.group(g);
- } catch (IllegalArgumentException iae) {
+ } catch (IllegalArgumentException x) {
//iae.printStackTrace();
- return;
- } catch (NullPointerException npe) {
- return;
+ try {
+ m.start(g);
+ } catch (IllegalArgumentException xx) {
+ try {
+ m.start(g);
+ } catch (IllegalArgumentException xxx) {
+ return;
+ }
+ }
}
failCount++;
}
+ private static void checkExpectedNPE(Matcher m) {
+ m.find();
+ try {
+ m.group(null);
+ } catch (NullPointerException x) {
+ try {
+ m.start(null);
+ } catch (NullPointerException xx) {
+ try {
+ m.end(null);
+ } catch (NullPointerException xxx) {
+ return;
+ }
+ }
+ }
+ failCount++;
+ }
private static void namedGroupCaptureTest() throws Exception {
check(Pattern.compile("x+(?