在阅读this post后,我知道我需要使用反引号(`)来包装我的正则表达式模式。现在我有正则表达式/^(?=^.{8,}$)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s)[[email protected]#$%^&*()]*$/
来检查密码模式是否正确。我已经用PHP测试过它,它工作正常。但它在Go中不起作用。为什么?正则表达式检查PHP的passwork工作,但不工作去
顺便说一句,反引号(`)变量的类型是什么?它似乎不是string
类型。我怎样才能声明这个变量容器?
测试代码
package main
import(
"fmt"
"regexp"
)
func main(){
re := regexp.MustCompile(`/^(?=^.{8,}$)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s)[[email protected]#$%^&*()]*$/`)
fmt.Println(re.MatchString("aSfd46Fgwaq"))
}
测试结果
Running...
panic: regexp: Compile(`/^(?=^.{8,}$)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s)[[email protected]#$%^&*()]*$/`): 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.
谢谢!
转到正则表达式不支持lookarounds和正则表达式的分隔符。 –
是否有任何其他插件,我可以'从github'获取'用于这个正则表达式模式? – Casper
'(?!。* \ s)'lookahead是冗余的,因为消费模式不匹配空格。 –