From 612f2586c3a730a4bcdfc6e7b958c0b37bf9de3e Mon Sep 17 00:00:00 2001 From: sherman Date: Wed, 21 Oct 2009 11:40:40 -0700 Subject: [PATCH] 6878475: Better syntax for the named capture group in regex Summary: Updated the syntax of the newly added named capture group Reviewed-by: martin, alanb --- .../classes/java/util/regex/Matcher.java | 14 +++++--- .../classes/java/util/regex/Pattern.java | 4 +-- test/java/util/regex/RegExTest.java | 35 ++++++++++--------- 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/src/share/classes/java/util/regex/Matcher.java b/src/share/classes/java/util/regex/Matcher.java index 488e47da8..76883d930 100644 --- a/src/share/classes/java/util/regex/Matcher.java +++ b/src/share/classes/java/util/regex/Matcher.java @@ -688,7 +688,7 @@ public final class Matcher implements MatchResult { * *

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 @@ public final class Matcher implements MatchResult { // 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 @@ public final class Matcher implements MatchResult { 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 { diff --git a/src/share/classes/java/util/regex/Pattern.java b/src/share/classes/java/util/regex/Pattern.java index bda83849a..a69c8da66 100644 --- a/src/share/classes/java/util/regex/Pattern.java +++ b/src/share/classes/java/util/regex/Pattern.java @@ -484,7 +484,7 @@ import java.util.Arrays; *

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. * *