Skip to content

Instantly share code, notes, and snippets.

@yehgdotnet
Created August 2, 2020 02:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yehgdotnet/9779505f52d9138aae39dc5ae4de224b to your computer and use it in GitHub Desktop.
Save yehgdotnet/9779505f52d9138aae39dc5ae4de224b to your computer and use it in GitHub Desktop.
Golang Extract IP from file (raw.txt)
package main
import (
"fmt"
"regexp"
"io/ioutil"
"log"
)
func main() {
content, err := ioutil.ReadFile("raw.txt")
if err != nil {
log.Fatal(err)
}
str1 := string(content)
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)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment