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
What is Rune? How to get ASCII value of any character in Go?
How to convert Go struct to JSON?
How to fetch an Integer variable as String in Go?
How to fix race condition using Atomic Functions in Golang?
Print index and element or data from Array, Slice and Map
Example: Stack and Caller from RUNTIME package