Skip to content

Commit

Permalink
Add support for (?<name>expr).
Browse files Browse the repository at this point in the history
This follows google/re2@6148386 (and
golang/go@ee61186) to some extent.
  • Loading branch information
junyer authored and sjamesr committed Aug 21, 2023
1 parent e3c736d commit 9b3f052
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
15 changes: 8 additions & 7 deletions java/com/google/re2j/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -1053,21 +1053,22 @@ private void parsePerlFlags(StringIterator t) throws PatternSyntaxException {
// support all three as well. EcmaScript 4 uses only the Python form.
//
// In both the open source world (via Code Search) and the
// Google source tree, (?P<expr>name) is the dominant form,
// so that's the one we implement. One is enough.
// Google source tree, (?P<name>expr) and (?<name>expr) are the
// dominant forms of named captures and both are supported.
String s = t.rest();
if (s.startsWith("(?P<")) {
if (s.startsWith("(?P<") || s.startsWith("(?<")) {
// Pull out name.
int end = s.indexOf('>');
int begin = s.charAt(2) == 'P' ? 4 : 3;
int end = s.indexOf('>', begin);
if (end < 0) {
throw new PatternSyntaxException(ERR_INVALID_NAMED_CAPTURE, s);
}
String name = s.substring(4, end); // "name"
String name = s.substring(begin, end); // "name"
t.skipString(name);
t.skip(5); // "(?P<>"
t.skip(begin + 1); // "(?P<>" or "(?<>"
if (!isValidCaptureName(name)) {
throw new PatternSyntaxException(
ERR_INVALID_NAMED_CAPTURE, s.substring(0, end)); // "(?P<name>"
ERR_INVALID_NAMED_CAPTURE, s.substring(0, end + 1)); // "(?P<name>" or "(?<name>"
}
// Like ordinary capture, but named.
Regexp re = op(Regexp.Op.LEFT_PAREN);
Expand Down
10 changes: 10 additions & 0 deletions javatests/com/google/re2j/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ public boolean applies(int r) {

// Test named captures
{"(?P<name>a)", "cap{name:lit{a}}"},
{"(?<name>a)", "cap{name:lit{a}}"},

// Case-folded literals
{"[Aa]", "litfold{A}"},
Expand Down Expand Up @@ -530,12 +531,20 @@ private static String runesToString(int[] runes) {
"(?P<name",
"(?P<x y>a)",
"(?P<>a)",
"(?<name>a",
"(?<name>",
"(?<name",
"(?<x y>a)",
"(?<>a)",
"[a-Z]",
"(?i)[a-Z]",
"a{100000}",
"a{100000,}",
// Group names may not be repeated
"(?P<foo>bar)(?P<foo>baz)",
"(?P<foo>bar)(?<foo>baz)",
"(?<foo>bar)(?P<foo>baz)",
"(?<foo>bar)(?<foo>baz)",
"\\x", // https://github.com/google/re2j/issues/103
"\\xv", // https://github.com/google/re2j/issues/103
};
Expand All @@ -550,6 +559,7 @@ private static String runesToString(int[] runes) {
"\\Q\\\\\\\\\\E",
"(?:a)",
"(?P<name>a)",
"(?<name>a)",
};

private static final String[] ONLY_POSIX = {
Expand Down

0 comments on commit 9b3f052

Please sign in to comment.