Closed ijt closed 5 years ago
Adding this test to parse_test.go might be useful:
func Test_regexpQuery(t *testing.T) {
t.Run("does not make valid regexps not compile", func(t *testing.T) {
pats := []string {
`[\s\S]b`,
}
for _, pat := range pats {
t.Run(pat, func(t *testing.T) {
// The given pats should all be valid.
_, err := regexp.Compile(pat)
if err != nil {
t.Fatal(err)
}
// Check to see that regexpQuery doesn't somehow produce an invalid result.
rx2, err := regexpQuery(pat, false, false)
if err != nil {
t.Fatal(err)
}
_, err = regexp.Compile(rx2.String())
if err != nil {
t.Errorf("compiling stringified version of regexpQuery() output: %v", err)
}
})
}
})
}
I think it's actually a bug in the regexp library.
\s is space, \S is not space [\s\S] should not translate to '[^]' but to '.'
You're right!
package main
import (
"regexp/syntax"
"testing"
)
func TestRegexpParse(t *testing.T) {
flags := syntax.PerlX
pat := `[\s\S]b`
rx, err := syntax.Parse(pat, flags)
if err != nil {
t.Fatal(err)
}
if rx.String() != pat {
t.Error("syntax.Parse() = ", rx.String(), ", want ", pat)
}
}
[ ~/src/github.com/ijt/rx ] go test ./...
--- FAIL: TestRegexpParse (0.00s)
main_test.go:16: syntax.Parse() = [^]b , want [\s\S]b
FAIL
FAIL github.com/ijt/rx 0.006s
I'll file it upstream.
Oh, looks like you already filed it: https://github.com/golang/go/issues/31807.
this is fixed in golang.