编译正则表达式时出现恐慌

huangapple go评论91阅读模式
英文:

Panic when compiling a regular expression

问题

我正在为密码验证编写一个正则表达式,代码如下:

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

当我执行应用程序时,它会出现错误:

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

这个正则表达式在JavaScript和Ruby中有效,但在Go中无效。我在这里做错了什么?

英文:

I am writing a regular expression for password validation with the following code:

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

When I execute the application, it panics:

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

This regular expression works in Javascript and Ruby but not in Go. What I am do wrong here?

答案1

得分: 5

从文档中可以看到:

>所接受的正则表达式的语法与Perl、Python和其他语言使用的一般语法相同。更准确地说,它是RE2所接受的语法,并在http://code.google.com/p/re2/wiki/Syntax上进行了描述,除了\C。

RE2维基清楚地说明了:

>(?=re) 在匹配re之前的文本 (不支持)

另请参阅:

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

英文:

From the docs:

>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.

The RE2 wiki clearly states:

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

See also:

EDIT: if you really need PCRE features, you can use a binding package, e.g. https://github.com/tuxychandru/golang-pkg-pcre/. Otherwise, try to rethink your regexp and what it should match.

答案2

得分: 5

Golang的正则表达式语法与PCRE(php / javascript大部分使用的语法)不同。

来自https://code.google.com/p/re2/wiki/Syntax:

(?=re):在匹配re之前的文本(不支持)

//编辑

以下是一个不使用正则表达式检查密码的示例:

var ErrInvalidPassword = errors.New(`密码至少需要包含7个字母,至少1个数字,至少1个大写字母,至少1个特殊字符。`)

func VerifyPassword(pw string) error {
    if len(pw) < 10 {
        return ErrInvalidPassword
    }
    var num, lower, upper, spec bool
    for _, r := range pw {
        switch {
        case unicode.IsDigit(r):
            num = true
        case unicode.IsUpper(r):
            upper = true
        case unicode.IsLower(r):
            lower = true
        case unicode.IsSymbol(r), unicode.IsPunct(r):
            spec = true
        }
    }
    if num && lower && upper && spec {
        return nil
    }
    return ErrInvalidPassword
}

playground

英文:

Golang's Regexp syntax is different from PCRE's (which php / javascript uses for the most part).

From https://code.google.com/p/re2/wiki/Syntax:
>(?=re) before text matching re (NOT SUPPORTED)

//edit

Example of checking the password without RE:

var ErrInvalidPassword = errors.New(`The password should at least have 7 letters, at least 1 number, at least 1 upper case, at least 1 special character.`)

func VerifyPassword(pw string) error {
	if len(pw) &lt; 10 {
		return ErrInvalidPassword
	}
	var num, lower, upper, spec bool
	for _, r := range pw {
		switch {
		case unicode.IsDigit(r):
			num = true
		case unicode.IsUpper(r):
			upper = true
		case unicode.IsLower(r):
			lower = true
		case unicode.IsSymbol(r), unicode.IsPunct(r):
			spec = true
		}
	}
	if num &amp;&amp; lower &amp;&amp; upper &amp;&amp; spec {
		return nil
	}
	return ErrInvalidPassword
}

<kbd>playground</kbd>

huangapple
  • 本文由 发表于 2014年8月6日 19:30:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/25159236.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定