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
Regular Expression to get a string between parentheses in Golang
How to split a string on white-space?
Sierpinski Carpet in Go Programming Language
Regular expression to extract date(YYYY-MM-DD) from string
How to check if a string contains a numbers in Golang?
Converting Int data type to Float in Go
Regular expression for matching HH:MM time format in Golang
How to check if a string contains a white space in Golang?
How to use for and foreach loop?
How to trim leading and trailing white spaces of a string in Golang?