# HG changeset patch # User igerasim # Date 1518498366 28800 # Node ID 84b4ffbba8b03b2722baaf7fd35b9900740166ba # Parent 478e198da84ba88a6eeea588cb43a49a3300208b 8197462: Inconsistent exception messages for invalid capturing group names 8179608: Error in comment in Pattern.java Reviewed-by: sherman diff -r 478e198da84b -r 84b4ffbba8b0 src/java.base/share/classes/java/util/regex/Pattern.java --- a/src/java.base/share/classes/java/util/regex/Pattern.java Tue Feb 13 12:26:22 2018 +0800 +++ b/src/java.base/share/classes/java/util/regex/Pattern.java Mon Feb 12 21:06:06 2018 -0800 @@ -782,12 +782,9 @@ * arguments, they can also be passed as inline modifiers. * For example, the following statements have the same effect. *
-     * RegExp r1 = RegExp.compile("abc", Pattern.I|Pattern.M);
-     * RegExp r2 = RegExp.compile("(?im)abc", 0);
+     * Pattern p1 = Pattern.compile("abc", Pattern.CASE_INSENSITIVE|Pattern.MULTILINE);
+     * Pattern p2 = Pattern.compile("(?im)abc", 0);
      * 
- * - * The flags are duplicated so that the familiar Perl match flag - * names are available. */ /** @@ -2527,7 +2524,7 @@ throw error("\\k is not followed by '<' for named capturing group"); String name = groupname(read()); if (!namedGroups().containsKey(name)) - throw error("(named capturing group <"+ name+"> does not exit"); + throw error("named capturing group <" + name + "> does not exist"); if (create) { hasGroupRef = true; if (has(CASE_INSENSITIVE)) @@ -2922,13 +2919,11 @@ */ private String groupname(int ch) { StringBuilder sb = new StringBuilder(); - sb.append(Character.toChars(ch)); - while (ASCII.isLower(ch=read()) || ASCII.isUpper(ch) || - ASCII.isDigit(ch)) { - sb.append(Character.toChars(ch)); - } - if (sb.length() == 0) - throw error("named capturing group has 0 length name"); + if (!ASCII.isAlpha(ch)) + throw error("capturing group name does not start with a Latin letter"); + do { + sb.append((char) ch); + } while (ASCII.isAlnum(ch=read())); if (ch != '>') throw error("named capturing group is missing trailing '>'"); return sb.toString(); @@ -2974,7 +2969,7 @@ break; case '<': // (?)", + "\\k<" + groupName + ">")) { + try { + Pattern.compile(pat); + failCount++; + } catch (PatternSyntaxException e) { + if (!e.getMessage().startsWith( + "capturing group name does not start with a" + + " Latin letter")) { + failCount++; + } + } + } + } + // Invalid char in a group name + for (String groupName : List.of("a.", "b\u0040", "c\u005b", + "d\u0060", "e\u007b", "f\u0416")) { + for (String pat : List.of("(?<" + groupName + ">)", + "\\k<" + groupName + ">")) { + try { + Pattern.compile(pat); + failCount++; + } catch (PatternSyntaxException e) { + if (!e.getMessage().startsWith( + "named capturing group is missing trailing '>'")) { + failCount++; + } + } + } + } + report("Invalid capturing group names"); + } }