Add names to ReplaceAll syntax
This commit is contained in:
parent
a937a67bab
commit
06785bac1a
6
pcre.go
6
pcre.go
@ -370,7 +370,8 @@ func (r *Regexp) NumSubexp() int {
|
|||||||
// regular expression with the replacement text repl.
|
// regular expression with the replacement text repl.
|
||||||
// Inside repl, $ signs are interpreted as in Expand,
|
// Inside repl, $ signs are interpreted as in Expand,
|
||||||
// so for instance $1 represents the text of the first
|
// 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 {
|
func (r *Regexp) ReplaceAll(src, repl []byte) []byte {
|
||||||
matches, err := r.match(src, 0, true)
|
matches, err := r.match(src, 0, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -388,8 +389,11 @@ func (r *Regexp) ReplaceAll(src, repl []byte) []byte {
|
|||||||
replStr := os.Expand(string(repl), func(s string) string {
|
replStr := os.Expand(string(repl), func(s string) string {
|
||||||
i, err := strconv.Atoi(s)
|
i, err := strconv.Atoi(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
i = r.SubexpIndex(s)
|
||||||
|
if i == -1 {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// If there given match does not exist, return empty string
|
// If there given match does not exist, return empty string
|
||||||
if i == 0 || len(match) < (2*i)+1 {
|
if i == 0 || len(match) < (2*i)+1 {
|
||||||
|
Loading…
Reference in New Issue
Block a user