After reading this post, I know I need to use a backquotes ( ` ) to wrap my regex pattern. Now I have regex pattern /^(?=^.{8,}$)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s)[0-9a-zA-Z!@#$%^&*()]*$/
to check password pattern whether is correct. I have tested it with PHP and it work correctly. But it does not work in Go. Why?
By the way, what is the type of backquoted ( ` ) variable? it seems not string
type. How can I declare this variable container?
Testing Code
package main
import(
"fmt"
"regexp"
)
func main(){
re := regexp.MustCompile(`/^(?=^.{8,}$)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s)[0-9a-zA-Z!@#$%^&*()]*$/`)
fmt.Println(re.MatchString("aSfd46Fgwaq"))
}
Testing Result
Running...
panic: regexp: Compile(`/^(?=^.{8,}$)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s)[0-9a-zA-Z!@#$%^&*()]*$/`): error parsing regexp: invalid or unsupported Perl syntax: `(?=`
goroutine 1 [running]:
panic(0x4efae0, 0xc82000a340)
/usr/local/go/src/runtime/panic.go:481 +0x3e6
regexp.MustCompile(0x576140, 0x4b, 0x100000000)
/usr/local/go/src/regexp/regexp.go:232 +0x16f
main.main()
/home/casper/.local/share/data/liteide/goplay.go:9 +0x30
exit status 2
Error: process exited with code 1.
Thank you!