为什么我的正则表达式在Go中总是失败?

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

Why does my regular expression ALWAYS fail in Go?

问题

我知道这里有很多关于为什么正则表达式失败的问题,但我没有看到有关Go/Golang的问题。

我有一个ID类型,它是一个带有一些附加方法的字符串:type ID string,并且有一个函数,如果ID有效,则应返回一个布尔值。

我对有效性的标准是:

  • 长度为20个字符。
  • 任何大写字母A-Z。
  • 任何数字0-9。

所以我的正则表达式,我在几个不同的在线工具上进行了测试,是/[A-Z0-9]{20}/

但是每次运行我的验证函数时,它都返回false,无论输入是什么。具体来说,regexp.MatchString()函数返回false和nil。

下面是我的验证函数,无论如何,match始终为false,即使我将正则表达式更改为/.{20}/,它应该匹配任意20个字符。

func (this ID) validate() bool {
    regexString := "/[A-Z0-9]{" + strconv.Itoa(MAX_ID_LENGTH) + "}/"
    fmt.Println(regexString)
    thisStr := this.ToString()
    fmt.Println(thisStr)
    charCount := utf8.RuneCountInString(thisStr)
    fmt.Println(charCount)
    thisStr = strings.ToUpper(thisStr)
    fmt.Println(thisStr)
    match, err := regexp.MatchString(regexString, thisStr)
    fmt.Println(match, err)

    // 检查字符串长度是否正确并匹配正则表达式。
    if charCount != MAX_ID_LENGTH || !match {
        return false
    } else {
        return true
    }
}

这是我使用函数的方式有问题吗?我被难住了。

**注意:**存在.ToUpper()函数,因为当生成我的ID时,它们只包含大写字母和0-9,但是如果以后手动输入"abc",我希望它被视为"ABC"。

英文:

I know there are plenty of questions on here asking why their regex fails, but I can't see any specifically for Go/Golang.

I have an ID type which is a string with some methods tacked on: type ID string, and this function which is supposed to return a bool if the ID is valid.

My criteria for validity are:

  • 20 characters long.
  • Any upper case letter A-Z.
  • Any digit 0-9.

So my regular expression, which I've tested on a few different online tools is /[A-Z0-9]{20}/

But every time I run my validate function it returns false, regardless of input. Specifically, the regexp.MatchString() function is returning false and nil.

Below is my validation function, match is ALWAYS false, even when I change the regex to /.{20}/ which is supposed to match 20 of anything.

func (this ID) validate() bool {

    regexString := "/[A-Z0-9]{" + strconv.Itoa(MAX_ID_LENGTH) + "}/"
    fmt.Println(regexString)

    thisStr := this.ToString()
    fmt.Println(thisStr)
    charCount := utf8.RuneCountInString(thisStr)
    fmt.Println(charCount)
    thisStr = strings.ToUpper(thisStr)
    fmt.Println(thisStr)
    match, err := regexp.MatchString(regexString, thisStr)
    fmt.Println(match, err)

    //Check	string length is correct and matches regex.
    if charCount != MAX_ID_LENGTH || !match {
	    return false
    } else {
    	return true
    }
}

Is it to do with the way I'm using the function? I'm stumped.

Note: the .ToUpper() function is there because when my IDs are generated they are only produced with upper case letters and 0-9, but I want "abc" to be treated as "ABC" if typed manually at a later date.

答案1

得分: 4

Go正则表达式不应该用/字符包围。去掉这些字符,看看是否有效。

/字符在某些语言(如JavaScript和Ruby)中用于创建正则表达式字面量,因为在正则表达式中\字符经常被使用,并且在常规字符串中需要双重转义。这只是某些语言中的一种便利方式,但通常不是实际正则表达式语法的一部分(尽管每种语言对正则表达式的处理方式略有不同)。

Go语言不支持正则表达式字面量,但它有字符串字面量的概念,这使得编写复杂的正则表达式变得更容易。

因此,例如,要查找字符串\.hidden-file-009,可以将表达式写为:

"\\\\\\.[a-zA-Z0-9\\-_]*"

或者更简单地写为:

`\\\.[a-zA-Z0-9\-_]*`
英文:

Go regular expressions should not be surrounded with / characters. Remove those and see if that works.

The / characters are used in some languages (such as javascript and ruby) when creating regular expression literals because the \ character is used frequently in regex and requires double escaping when in a regular string. It's just a convenience in some languages, but typically is not part of the actual regex syntax (though every language handles regex a bit differently).

The Go language does not support regex literals, but it does have the concept of string literals, which makes it much easier to write complex regular expressions.

So, for example, to find the string \.hidden-file-009, you could write the expression as:

"\\\\\\.[a-zA-Z0-9\\-_]*"

or, more simply:

 `\\\.[a-zA-Z0-9\-_]*`

huangapple
  • 本文由 发表于 2015年4月2日 22:45:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/29415857.html
匿名

发表评论

匿名网友

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

确定