regex - 编译正则表达式时出现 panic

标签 regex go

我正在使用以下代码编写用于密码验证的正则表达式:

func IsPasswordValid(value string) bool {
    pattern := regexp.MustCompile(`^.*(?=.{7,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9]).*$`)
    return pattern.MatchString(value)
}

当我执行该应用程序时,它会出现困惑:

Line 45: - regexp: Compile(`^.*(?=.{7,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9]).*$`): error parsing regexp: invalid or unsupported Perl syntax: `(?=` 

此正则表达式适用于 Javascript 和 Ruby,但不适用于 Go。我在这里做错了什么?

最佳答案

来自文档:

The syntax of the regular expressions accepted is the same general syntax used by Perl, Python, and other languages. More precisely, it is the syntax accepted by RE2 and described at http://code.google.com/p/re2/wiki/Syntax, except for \C.

RE2 wiki 明确指出:

(?=re) before text matching re (NOT SUPPORTED)

另见:

编辑:如果您真的需要 PCRE 功能,您可以使用绑定(bind)包,例如https://github.com/tuxychandru/golang-pkg-pcre/ .否则,请尝试重新考虑您的正则表达式及其应匹配的内容。

关于regex - 编译正则表达式时出现 panic ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25159236/

相关文章:

正则表达式用于检测和替换具有重复捕获组的行,以删除 piprequirements.txt 文件中的重复包

regex - 根据其他列向 Panda 数据框添加新列

go - 如何用逗号和 2 位小数格式化货币?

json - 将 JSON 或映射从 map[string]interface{} 插入到 MongoDB 集合将 int 和 float 设置为字符串

Go Gorilla Mux MiddlewareFunc with r.Use 并返回错误

dictionary - 落在引用/指针上的类型断言

regex - 这个正则表达式如何找到素数?

regex - 按文本文件中的特定模式对行和列重新排序

arrays - 在 perl 中,为什么 push 导致由 qr 创建的正则表达式在未放入双引号时被更改?

进入 channel 无限循环不阻塞