Created
February 23, 2015 16:48
-
-
Save vladholubiev/ec091315abb8f77fe0ee to your computer and use it in GitHub Desktop.
Extract all IPv4 from .txt file
This file contains hidden or 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
| 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