aboutsummaryrefslogtreecommitdiff
path: root/src/regexp/syntax/parse.go
diff options
context:
space:
mode:
authorMauri de Souza Meneguzzo <mauri870@gmail.com>2023-07-29 20:26:00 +0000
committerGopher Robot <gobot@golang.org>2023-07-31 19:52:57 +0000
commitee61186b3301bb1c8610c0925fffd89e061909bb (patch)
tree81eb81f16c4f7b9a3e25c1eba3db67e965e555ce /src/regexp/syntax/parse.go
parent977e23a70766f8d0801e43bbfb68136bf54a84b7 (diff)
downloadgo-ee61186b3301bb1c8610c0925fffd89e061909bb.tar.xz
regexp/syntax: accept (?<name>...) syntax as valid capture
Currently the only named capture supported by regexp is (?P<name>a). The syntax (?<name>a) is also widely used and there is currently an effort from the Rust regex and RE2 teams to also accept this syntax. Fixes #58458 Change-Id: If22d44d3a5c4e8133ec68238ab130c151ca7c5c5 GitHub-Last-Rev: 31b50e6ab40cfb0f36df6f570525657d4680017f GitHub-Pull-Request: golang/go#61624 Reviewed-on: https://go-review.googlesource.com/c/go/+/513838 Auto-Submit: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: David Chase <drchase@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
Diffstat (limited to 'src/regexp/syntax/parse.go')
-rw-r--r--src/regexp/syntax/parse.go19
1 files changed, 14 insertions, 5 deletions
diff --git a/src/regexp/syntax/parse.go b/src/regexp/syntax/parse.go
index accee9ab08..a4ccfe3bdb 100644
--- a/src/regexp/syntax/parse.go
+++ b/src/regexp/syntax/parse.go
@@ -1159,9 +1159,18 @@ func (p *parser) parsePerlFlags(s string) (rest string, err error) {
// 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.
- if len(t) > 4 && t[2] == 'P' && t[3] == '<' {
+ // Google source tree, (?P<expr>name) and (?<expr>name) are the
+ // dominant forms of named captures and both are supported.
+ startsWithP := len(t) > 4 && t[2] == 'P' && t[3] == '<'
+ startsWithName := len(t) > 3 && t[2] == '<'
+
+ if startsWithP || startsWithName {
+ // position of expr start
+ exprStartPos := 4
+ if startsWithName {
+ exprStartPos = 3
+ }
+
// Pull out name.
end := strings.IndexRune(t, '>')
if end < 0 {
@@ -1171,8 +1180,8 @@ func (p *parser) parsePerlFlags(s string) (rest string, err error) {
return "", &Error{ErrInvalidNamedCapture, s}
}
- capture := t[:end+1] // "(?P<name>"
- name := t[4:end] // "name"
+ capture := t[:end+1] // "(?P<name>" or "(?<name>"
+ name := t[exprStartPos:end] // "name"
if err = checkUTF8(name); err != nil {
return "", err
}