我正在嘗試在 Go 中編寫一個函數,它將替換foobar為foobaz,但前提是bar前面是foo。我嘗試了以下方法:package mainimport (? ? "fmt"? ? "regexp")func main() {? ? re := regexp.MustCompile(`(?<=foo)bar`)? ? new := re.ReplaceAll([]byte("foobar"), []byte("baz"))? ? fmt.Println(string(new))}目標是讓這個程序 print foobaz,但我卻感到恐慌,因為正則表達式無法編譯:panic: regexp: Compile(`(?<=foo)bar`): error parsing regexp: invalid or unsupported Perl syntax: `(?<`我怎樣才能做到這一點?筆記我嘗試過的一件事是將正則表達式替換為非捕獲組:re := regexp.MustCompile(`(?:foo)bar`)但是,我發現程序隨后打印baz而不是按foobaz預期打印。
- 1 回答
- 0 關注
- 186 瀏覽
添加回答
舉報
0/150
提交
取消