I need your help for one tricky problem. I have one map like below
Map<Integer, String> ruleMap = new HashMap<>();
ruleMap.put(1, "A");
ruleMap.put(12, "Test - 12");
ruleMap.put(1012, "Test Metadata 12 A");
and I have one rule in which I am separating ids and collecting them into a list. By iterating over the list, I am replacing its ids by its respective value from map.
code :
String rule = "1 AND 12 OR 1012 AND 12 or 1012";
List < String > idList = new ArrayList < > ();
Pattern p = Pattern.compile("[0-9]+");
Matcher m = p.matcher(rule);
while (m.find()) {
idList.add(m.group());
}
for (String id: idList) {
if (ObjectUtils.isNonEmpty(ruleMap.get(Integer.parseInt(id)))) {
rule = rule.replaceFirst(id, ruleMap.get(Integer.parseInt(id)));
}
}
System.out.println(rule);
I am getting output like this -
A AND Test - Test - 12 OR Test Metadata 12 A AND 12 or Test Metadata 12 A
as you can see for the first iteration of id 12 replaced its respected value but on the second occurrence of the id 12 replaced the value Test - 12 to Test - Test - 12.
So can anyone help me with this?