I'm afraid your code looks pretty efficient. Here is my version:
Matcher match = Pattern .compile("(\\w+)\\s+\"(\\d+\\.\\d+\\.\\d+\\.\\d+)\"") .matcher(lineInFile); while(match.find()) { //do something }
There are two microoptimizations:
- No need to save the template in an additional variable, underlined that
- For an alias, word search characters rather than space characters
In fact, if you process so much and the template never changes, you should save the compiled template in a constant:
private static final Pattern PATTERN = Pattern .compile("(\\w+)\\s+\"(\\d+\\.\\d+\\.\\d+\\.\\d+)\""); Matcher match = PATTERN.matcher(lineInFile); while(match.find()) {
Update: I spent some time on RegExr to create a much more specific template, which should only identify valid IP addresses as a bonus. I know this is ugly as hell, but I assume it is quite efficient as it eliminates most of the backtracking:
([AZ]+)\s*\"((?:1[0-9]{2}|2(?:(?:5[0-5]|[0-9]{2})|[0-9]{1,2})\.) {3}(?:1[0-9]{2}|2(?:5[0-5]|[0-9]{2})|[0-9]{1,2}))
(Wrapped for readability, all backslashes should be escaped in java, but you can test it in RegExr, as in the OP test string)
source share