Created
August 2, 2020 02:46
-
-
Save yehgdotnet/9779505f52d9138aae39dc5ae4de224b to your computer and use it in GitHub Desktop.
Golang Extract IP from file (raw.txt)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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