I'm using a pattern matching for string in Java. I have an issue, the CPU goes high and does nothing when trying to match the patterns. I have 100's of string which needs to be checked if it matches the 2 patterns.
Below is the sample code I'm using. It stops and CPU goes 100% for the first string (patternList) when matching it for the pattern 2 i.e patternMatch[1]. How can I make this better?
String[] patternMatch = {"([\\w\\s]+)+([+\\-/*])+([\\w\\s]+)",
"([\\w\\s]+)+([+\\-/*])+([\\w\\s]+)+([+\\-/*])+([\\w\\s]+)"};
List<String> patternList = new ArrayList<String>();
patternList.add("Avg Volume Units product A + Volume Units product A");
patternList.add("Avg Volume Units / Volume Units product A");
patternList.add("Avg retailer On Hand / Volume Units Plan / Store Count");
patternList.add("Avg Hand Volume Units Plan Store Count");
patternList.add("1 - Avg merchant Volume Units");
patternList.add("Total retailer shipment Count");
for (String s :patternList ){
for(int i=0;i<patternMatch.length;i++){
Pattern pattern = Pattern.compile(patternMatch[i]);
Matcher matcher = pattern.matcher(s);
System.out.println(s);
if (matcher.matches()) {
System.out.println("Passed");
}else
System.out.println("Failed;");
}
}
([\\w\\s]+)+? Why not([\\w\\s]+)? - Pshemo