I am attempting to use a regular expression with Scanner to match a string from a file. The regex works with all of the contents of the file except for this line:
As the others have said, your regex is much less efficient than it should be. I'd take it a step further and use possessive quantifiers:
"^([a-zA-Z]++) *+= *+\"([^\"]++)\"$"
But the way you're using the Scanner doesn't make much sense, either. There's no need to use findInLine(".*")
to read the line; that's what nextLine()
does. And you don't need to create another Scanner to apply your regex; just use a Matcher.
static final Pattern ANIMAL_INFO_PATTERN =
Pattern.compile("^([a-zA-Z]++) *+= *+\"([^\"]++)\"$");
...
Matcher lineMatcher = ANIMAL_INFO_PATTERN.matcher("");
while (scanFile.hasNextLine()) {
String currentLine = scanFile.nextLine();
if (lineMatcher.reset(currentLine).matches()) {
matches.put(lineMatcher.group(1), lineMatcher.group(2));
}
}