“未知转义序列”出错。

时间:2022-12-20 00:07:36

I have the following function written in Go. The idea is the function has a string passed to it and returns the first IPv4 IP address found. If no IP address is found, an empty string is returned.

下面的函数用Go表示。其思想是,函数有一个传递给它的字符串,并返回找到的第一个IPv4 IP地址。如果没有找到IP地址,则返回一个空字符串。

func parseIp(checkIpBody string) string {
    reg, err := regexp.Compile("[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+")
    if err == nil {
        return ""
    }   
    return reg.FindString(checkIpBody)
}

The compile-time error I'm getting is

我得到的编译时错误是

unknown escape sequence: .

未知的转义序列:。

How can I tell Go that the '.' is the actual character I'm looking for? I thought escaping it would do the trick, but apparently I'm wrong.

我怎么能告诉你那是什么。“是我要找的那个角色吗?”我原以为逃避就行,但显然我错了。

2 个解决方案

#1


70  

The \ backslash isn't being interpreted by the regex parser, it's being interpreted in the string literal. You should escape the backslash again:

\反斜杠不是由regex解析器解释的,而是用字符串文字解释的。你应该再次避开反斜线:

regexp.Compile("[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+")

A string quoted with " double-quote characters is known as an "interpreted string literal" in Go. Interpreted string literals are like string literals in most languages: \ backslash characters aren't included literally, they're used to give special meaning to the next character. The source must included \\ two backslashes in a row to obtain an a single backslash character in the parsed value.

引用“双引号字符”的字符串在Go中称为“解释字符串字面量”。解释字符串在大多数语言中都类似字符串:\反斜杠字符不包含在字面上,它们用于赋予下一个字符特殊的含义。源必须包含\\两个反斜杠,以在解析值中获得一个反斜杠字符。

As Evan Shaw pointed out in the comments, Go has another alternative which can be useful when writing string literals for regular expressions. A "raw string literal" is quoted by ` backtick characters. There are no special characters in a raw string literal, so as long as your pattern doesn't include a backtick you can use this syntax without escaping anything:

正如Evan Shaw在评论中指出的,Go还有另一种方法,在为正则表达式编写字符串常量时很有用。“原始字符串文字”被“后勾字符”引用。原始字符串中没有特殊的字符,所以只要您的模式不包含勾号,您就可以使用这个语法而不用转义:

regexp.Compile(`[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+`)

This is described in the "String literals" section of the Go spec.

这是在Go规范的“字符串文字”部分中描述的。

#2


1  

IPv4 address (accurate capture)

IPv4地址(精确捕捉)

Matches 0.0.0.0 through 255.255.255.255

匹配通过255.255.255.255 0.0.0.0

Use this regex to match IP numbers with accurracy.

使用此regex将IP数字与accurracy匹配。

Each of the 4 numbers is stored into a capturing group, so you can access them for further processing.

这4个数字中的每一个都存储在一个捕获组中,因此您可以访问它们以进行进一步的处理。

"(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])"

#1


70  

The \ backslash isn't being interpreted by the regex parser, it's being interpreted in the string literal. You should escape the backslash again:

\反斜杠不是由regex解析器解释的,而是用字符串文字解释的。你应该再次避开反斜线:

regexp.Compile("[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+")

A string quoted with " double-quote characters is known as an "interpreted string literal" in Go. Interpreted string literals are like string literals in most languages: \ backslash characters aren't included literally, they're used to give special meaning to the next character. The source must included \\ two backslashes in a row to obtain an a single backslash character in the parsed value.

引用“双引号字符”的字符串在Go中称为“解释字符串字面量”。解释字符串在大多数语言中都类似字符串:\反斜杠字符不包含在字面上,它们用于赋予下一个字符特殊的含义。源必须包含\\两个反斜杠,以在解析值中获得一个反斜杠字符。

As Evan Shaw pointed out in the comments, Go has another alternative which can be useful when writing string literals for regular expressions. A "raw string literal" is quoted by ` backtick characters. There are no special characters in a raw string literal, so as long as your pattern doesn't include a backtick you can use this syntax without escaping anything:

正如Evan Shaw在评论中指出的,Go还有另一种方法,在为正则表达式编写字符串常量时很有用。“原始字符串文字”被“后勾字符”引用。原始字符串中没有特殊的字符,所以只要您的模式不包含勾号,您就可以使用这个语法而不用转义:

regexp.Compile(`[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+`)

This is described in the "String literals" section of the Go spec.

这是在Go规范的“字符串文字”部分中描述的。

#2


1  

IPv4 address (accurate capture)

IPv4地址(精确捕捉)

Matches 0.0.0.0 through 255.255.255.255

匹配通过255.255.255.255 0.0.0.0

Use this regex to match IP numbers with accurracy.

使用此regex将IP数字与accurracy匹配。

Each of the 4 numbers is stored into a capturing group, so you can access them for further processing.

这4个数字中的每一个都存储在一个捕获组中,因此您可以访问它们以进行进一步的处理。

"(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])"

相关文章