Regular expression to extract DNS host-name or IP Address from string
Example
package main
import (
"fmt"
"regexp"
)
func main() {
str1 := `Proxy Port Last Check Proxy Speed Proxy Country Anonymity 118.99.81.204
118.99.81.204 8080 34 sec Indonesia - Tangerang Transparent 2.184.31.2 8080 58 sec
Iran Transparent 93.126.11.189 8080 1 min Iran - Esfahan Transparent 202.118.236.130
7777 1 min China - Harbin Transparent 62.201.207.9 8080 1 min Iraq Transparent`
re := regexp.MustCompile(`(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}`)
fmt.Printf("Pattern: %v\n", re.String()) // print pattern
fmt.Println(re.MatchString(str1)) // true
submatchall := re.FindAllString(str1, -1)
for _, element := range submatchall {
fmt.Println(element)
}
}
Output
Pattern: (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}
true
118.99.81.204
118.99.81.204
2.184.31.2
93.126.11.189
202.118.236.130
62.201.207.9
Most Helpful This Week
How to check pointer or interface is nil?
How to import and alias package names?
How to check string contains uppercase lowercase character in Golang?
How to wait for Goroutines to Finish Execution?
How to read names of all files and folders in current directory?
How pointer & and * and ** works in Golang?
How to check if a map contains a key in Go?
How do you write multi-line strings in Go?
How to get the current date and time with timestamp in local and other timezones ?
How to fix race condition using Atomic Functions in Golang?