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
Select single argument from all arguments of variadic function
How to copy a map to another map?
How to use array in Go Programming Language?
Example to create custom error
Dereferencing a pointer from another package
How to convert Boolean Type to String in Go?
Print index and element or data from Array, Slice and Map
How to read names of all files and folders in current directory?
Passing multiple string arguments to a variadic function
How to delete or remove element from a Map?