"Unknown escape sequence"错误在Go中

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

"Unknown escape sequence" error in Go

问题

我在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)
}

我得到的编译时错误是

未知的转义序列: .

我该如何告诉Go . 是我要寻找的实际字符?我以为转义它会起作用,但显然我错了。

英文:

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.

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.

答案1

得分: 187

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

<!-- language-all: go -->

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 include \\ two backslashes in a row to obtain an a single backslash character in the parsed value.

Go has another alternative which can be useful when writing string literals for regular expressions: a "raw string literal" is quoted by <code> 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:

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

These are described in the "String literals" section of the Go spec.

英文:

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

<!-- language-all: go -->

regexp.Compile(&quot;[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+&quot;)

A string quoted with &quot; 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 include \\ two backslashes in a row to obtain an a single backslash character in the parsed value.

Go has another alternative which can be useful when writing string literals for regular expressions: a "raw string literal" is quoted by <code>`</code> 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:

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

These are described in the "String literals" section of the Go spec.

答案2

得分: 1

"(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])"

英文:

IPv4 address (accurate capture)

Matches 0.0.0.0 through 255.255.255.255

Use this regex to match IP numbers with accurracy.

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

&quot;(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])&quot;

huangapple
  • 本文由 发表于 2011年7月21日 11:16:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/6770898.html
匿名

发表评论

匿名网友

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

确定