Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to replace on match group? #8

Open
mdwhatcott opened this issue May 5, 2016 · 2 comments
Open

How to replace on match group? #8

mdwhatcott opened this issue May 5, 2016 · 2 comments

Comments

@mdwhatcott
Copy link

Consider the following interactions, which I assumed would produce identical output, but which do not:

r1 := regexp.MustCompile("a(sd)f")
m1 := r1.ReplaceAll([]byte("asdf"), []byte(" $1 "))
fmt.Printf("[%s]\n", m1) // this produces the correct output: [ sd ]

r2 := pcre.MustCompile("a(sd)f", 0)
m2 := r2.ReplaceAll([]byte("asdf"), []byte(" $1 "), 0)
fmt.Printf("[%s]\n", m2) // this produces incorrect output: [ $1 ]

The first three lines illustrate the behavior of the standard library. The next three illustrate the behavior of this package.

Is there a way to replace on a match group using this library?

@ye-lin-aung
Copy link

I got the same issue.Was it solved ?

@eric-s-raymond
Copy link

The PCRE syntax for a backreference is supposed to be \1 rather than $1, but that doesn't work either.

package main

import (
	"fmt"
	pcre "github.com/glenn-brown/golang-pkg-pcre/src/pkg/pcre"
)

func main () {
	from := []byte("aaabbbcccc")
	pattern := "b(.)b"
	re := pcre.MustCompile(pattern, 0)
	result := re.ReplaceAll(from, []byte("ddd\\1"), 0)
	
	fmt.Printf("Result is: %s\n", result)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants