//using java regex, re{n,m} means repeat re at least n time at most m times
public static List<String> findUniqueIPs(String fileName) {
Pattern p = Pattern.compile("(\\d{1,3}\\.){3}(\\d{1,3})");
Set<String> hs = new HashSet<String>();
try {
Scanner scanner = new Scanner(new File(fileName));
while(scanner.hasNext()) {
String line = scanner.nextLine();
Matcher m = p.matcher(line);
while(m.find()) {
hs.add(m.group());
}
}
} catch(Exception e) { }
List<String> ans = new ArrayList<String>();
ans.addAll(hs);
return ans;
}
[LinkedIn, LeetCode] given a log file, parse the valid ip addresses, no duplicate
最新推荐文章于 2024-05-30 19:57:38 发布