“Unknown escape sequence” error in Go

后端 未结 2 1218
说谎
说谎 2020-11-28 05:28

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

相关标签:
2条回答
  • 2020-11-28 05:53

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

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

    0 讨论(0)
  • 2020-11-28 05:53

    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.

    "(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])"
    
    0 讨论(0)
提交回复
热议问题