dlclark / regexp2

A full-featured regex engine in pure Go based on the .NET engine
MIT License
997 stars 84 forks source link

Wrong first reference in Replace() #8

Closed vkd closed 7 years ago

vkd commented 7 years ago

Test case:

func TestReplaceRef(t *testing.T) {
    re := MustCompile("(123)hello(789)", None)
    res, err := re.Replace("123hello789", "$1456$2", -1, -1)
    if err != nil {
        t.Fatal(err)
    }
    if res != "123456789" {
        t.Fatalf("Wrong result: %s", res)
    }
}

Result:

--- FAIL: TestReplaceRef (0.00s)
    regexp_test.go:775: Wrong result: $1456789
dlclark commented 7 years ago

I've confirmed that this is working as-designed. It behaves exactly the same as the .NET regex engine. If you want to do this kind of replacement text (where the $1 is followed without spaces by another number) you need to add braces so the parser can tell where the intended group number stops (e.g. ${1}456$2)