From 06785bac1a21fccc38f32270ebc8e41385143290 Mon Sep 17 00:00:00 2001 From: Elara Musayelyan Date: Sat, 21 May 2022 11:45:30 -0700 Subject: [PATCH] Add names to ReplaceAll syntax --- pcre.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pcre.go b/pcre.go index a718de7..f46fb1f 100644 --- a/pcre.go +++ b/pcre.go @@ -370,7 +370,8 @@ func (r *Regexp) NumSubexp() int { // regular expression with the replacement text repl. // Inside repl, $ signs are interpreted as in Expand, // so for instance $1 represents the text of the first -// submatch. +// submatch and $name would represent the text of the +// subexpression called "name". func (r *Regexp) ReplaceAll(src, repl []byte) []byte { matches, err := r.match(src, 0, true) if err != nil { @@ -388,7 +389,10 @@ func (r *Regexp) ReplaceAll(src, repl []byte) []byte { replStr := os.Expand(string(repl), func(s string) string { i, err := strconv.Atoi(s) if err != nil { - return "" + i = r.SubexpIndex(s) + if i == -1 { + return "" + } } // If there given match does not exist, return empty string