Skip to content

Instantly share code, notes, and snippets.

@vladholubiev
Created February 23, 2015 16:48
Show Gist options
  • Select an option

  • Save vladholubiev/ec091315abb8f77fe0ee to your computer and use it in GitHub Desktop.

Select an option

Save vladholubiev/ec091315abb8f77fe0ee to your computer and use it in GitHub Desktop.
Extract all IPv4 from .txt file
public static void main(String[] args) throws IOException {
System.out.println("Welcome to IP-EXTRACTOR");
System.out.println("Make sure you have ip.txt file in the same directory");
System.out.println("Press Enter to start");
Scanner scanner = new Scanner(System.in);
scanner.nextLine();
Pattern ipPattern = Pattern.compile("\\b(?:[0-9]{1,3}\\.){3}[0-9]{1,3}\\b");
PrintWriter printWriter = new PrintWriter("result.txt");
List<String> strings = Files.readAllLines(Paths.get("ip.txt"), Charset.defaultCharset());
for (String line : strings) {
Matcher matcher = ipPattern.matcher(line);
while (matcher.find()) {
String ip = matcher.group();
printWriter.println(ip);
}
}
printWriter.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment