'"Unknown escape sequence" error in 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.
Solution 1:[1]
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])"
Solution 2:[2]
You can use the following. You can of course change the post type according to your needs.
add_action( 'init', function() {
remove_post_type_support( 'post', 'editor' );
remove_post_type_support( 'page', 'editor' );
}, 99);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Monday |
| Solution 2 | Khalil |
