--- old/src/share/classes/java/util/regex/Pattern.java Tue Oct 20 14:58:32 2009
+++ new/src/share/classes/java/util/regex/Pattern.java Tue Oct 20 14:58:27 2009
@@ -484,7 +484,7 @@
*
Group name
* A capturing group can also be assigned a "name", a named-capturing group,
* and then be back-referenced later by the "name". Group names are composed of
- * the following characters:
+ * the following characters. The first character must be a letter.
*
*
* - The uppercase letters 'A' through 'Z'
@@ -2567,7 +2567,7 @@
break;
case '<': // (? The replacement string may contain references to subsequences
* captured during the previous match: Each occurrence of
- * $<name> or $g
+ * ${name} or $g
* will be replaced by the result of evaluating the corresponding
* {@link #group(String) group(name)} or {@link #group(int) group(g)}
* respectively. For $g,
@@ -770,7 +770,7 @@
// more appropriate.
nextChar = replacement.charAt(cursor);
int refNum = -1;
- if (nextChar == '<') {
+ if (nextChar == '{') {
cursor++;
StringBuilder gsb = new StringBuilder();
while (cursor < replacement.length()) {
@@ -787,13 +787,17 @@
if (gsb.length() == 0)
throw new IllegalArgumentException(
"named capturing group has 0 length name");
- if (nextChar != '>')
+ if (nextChar != '}')
throw new IllegalArgumentException(
- "named capturing group is missing trailing '>'");
+ "named capturing group is missing trailing '}'");
String gname = gsb.toString();
+ if (ASCII.isDigit(gname.charAt(0)))
+ throw new IllegalArgumentException(
+ "capturing group name {" + gname +
+ "} starts with digit character");
if (!parentPattern.namedGroups().containsKey(gname))
throw new IllegalArgumentException(
- "No group with name <" + gname + ">");
+ "No group with name {" + gname + "}");
refNum = parentPattern.namedGroups().get(gname);
cursor++;
} else {
--- old/test/java/util/regex/RegExTest.java Tue Oct 20 14:59:08 2009
+++ new/test/java/util/regex/RegExTest.java Tue Oct 20 14:59:03 2009
@@ -32,7 +32,7 @@
* 4872664 4803179 4892980 4900747 4945394 4938995 4979006 4994840 4997476
* 5013885 5003322 4988891 5098443 5110268 6173522 4829857 5027748 6376940
* 6358731 6178785 6284152 6231989 6497148 6486934 6233084 6504326 6635133
- * 6350801 6676425
+ * 6350801 6676425 6878475
*/
import java.util.regex.*;
@@ -3389,9 +3389,9 @@
"gname",
"yyy");
- check(Pattern.compile("x+(?<8gname>y+)z+"),
+ check(Pattern.compile("x+(?y+)z+"),
"xxxyyyzzz",
- "8gname",
+ "gname8",
"yyy");
//backref
@@ -3430,33 +3430,33 @@
//replaceFirst/All
checkReplaceFirst("(?ab)(c*)",
"abccczzzabcczzzabccc",
- "$",
+ "${gn}",
"abzzzabcczzzabccc");
checkReplaceAll("(?ab)(c*)",
"abccczzzabcczzzabccc",
- "$",
+ "${gn}",
"abzzzabzzzab");
checkReplaceFirst("(?ab)(c*)",
"zzzabccczzzabcczzzabccczzz",
- "$",
+ "${gn}",
"zzzabzzzabcczzzabccczzz");
checkReplaceAll("(?ab)(c*)",
"zzzabccczzzabcczzzabccczzz",
- "$",
+ "${gn}",
"zzzabzzzabzzzabzzz");
checkReplaceFirst("(?ab)(?c*)",
"zzzabccczzzabcczzzabccczzz",
- "$",
+ "${gn2}",
"zzzccczzzabcczzzabccczzz");
checkReplaceAll("(?ab)(?c*)",
"zzzabccczzzabcczzzabccczzz",
- "$",
+ "${gn2}",
"zzzccczzzcczzzccczzz");
//toSupplementaries("(ab)(c*)"));
@@ -3463,7 +3463,7 @@
checkReplaceFirst("(?" + toSupplementaries("ab") +
")(?" + toSupplementaries("c") + "*)",
toSupplementaries("abccczzzabcczzzabccc"),
- "$",
+ "${gn1}",
toSupplementaries("abzzzabcczzzabccc"));
@@ -3470,13 +3470,13 @@
checkReplaceAll("(?" + toSupplementaries("ab") +
")(?" + toSupplementaries("c") + "*)",
toSupplementaries("abccczzzabcczzzabccc"),
- "$",
+ "${gn1}",
toSupplementaries("abzzzabzzzab"));
checkReplaceFirst("(?" + toSupplementaries("ab") +
")(?" + toSupplementaries("c") + "*)",
toSupplementaries("abccczzzabcczzzabccc"),
- "$",
+ "${gn2}",
toSupplementaries("ccczzzabcczzzabccc"));
@@ -3483,28 +3483,29 @@
checkReplaceAll("(?" + toSupplementaries("ab") +
")(?" + toSupplementaries("c") + "*)",
toSupplementaries("abccczzzabcczzzabccc"),
- "$",
+ "${gn2}",
toSupplementaries("ccczzzcczzzccc"));
checkReplaceFirst("(?Dog)AndCat",
"zzzDogAndCatzzzDogAndCatzzz",
- "$",
+ "${dog}",
"zzzDogzzzDogAndCatzzz");
checkReplaceAll("(?Dog)AndCat",
"zzzDogAndCatzzzDogAndCatzzz",
- "$",
+ "${dog}",
"zzzDogzzzDogzzz");
// backref in Matcher & String
- if (!"abcdefghij".replaceFirst("cd(?ef)gh", "$").equals("abefij") ||
- !"abbbcbdbefgh".replaceAll("(?[a-e])b", "$").equals("abcdefgh"))
+ if (!"abcdefghij".replaceFirst("cd(?ef)gh", "${gn}").equals("abefij") ||
+ !"abbbcbdbefgh".replaceAll("(?[a-e])b", "${gn}").equals("abcdefgh"))
failCount++;
// negative
checkExpectedFail("(?abc)(def)");
checkExpectedFail("(?abc)(def)");
+ checkExpectedFail("(?<6groupnamestartswithdigit>abc)(def)");
checkExpectedFail("(?abc)(def)\\k");
checkExpectedFail("(?abc)(?def)\\k");
checkExpectedFail(Pattern.compile("(?abc)(def)").matcher("abcdef"),