From 3fabeb787ab370235d9103b4d35d5a2c9e860a0b Mon Sep 17 00:00:00 2001 From: Villu Ruusmann Date: Thu, 21 Nov 2024 07:28:01 +0200 Subject: [PATCH] Fixed the evaluation of 'replace' built-in function --- .../java/org/jpmml/evaluator/Functions.java | 3 +++ .../org/jpmml/evaluator/FunctionTest.java | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/pmml-evaluator/src/main/java/org/jpmml/evaluator/Functions.java b/pmml-evaluator/src/main/java/org/jpmml/evaluator/Functions.java index c234ec06..7f51fa8e 100644 --- a/pmml-evaluator/src/main/java/org/jpmml/evaluator/Functions.java +++ b/pmml-evaluator/src/main/java/org/jpmml/evaluator/Functions.java @@ -585,6 +585,9 @@ public String evaluate(String input, String regex, String replacement){ Matcher matcher = pattern.matcher(input); + // Replace PCRE-style dollar literal with Java-style dollar literal + replacement = replacement.replace("$$", "\\$"); + return matcher.replaceAll(replacement); } diff --git a/pmml-evaluator/src/test/java/org/jpmml/evaluator/FunctionTest.java b/pmml-evaluator/src/test/java/org/jpmml/evaluator/FunctionTest.java index fc03c460..76474f0d 100644 --- a/pmml-evaluator/src/test/java/org/jpmml/evaluator/FunctionTest.java +++ b/pmml-evaluator/src/test/java/org/jpmml/evaluator/FunctionTest.java @@ -407,6 +407,26 @@ public void evaluateRegularExpressionFunctions(){ assertEquals("c", evaluate(Functions.REPLACE, "BBBB", "B+", "c")); assertEquals("cccc", evaluate(Functions.REPLACE, "BBBB", "B+?", "c")); + try { + evaluate(Functions.REPLACE, "BBBB", "B", "$"); + + fail(); + } catch(IllegalArgumentException iae){ + // Ignored + } + + assertEquals("$$$$", evaluate(Functions.REPLACE, "BBBB", "B", "$$")); + + try { + evaluate(Functions.REPLACE, "BBBB", "B", "\\"); + + fail(); + } catch(IllegalArgumentException iae){ + // Ignored + } + + assertEquals("\\\\\\\\", evaluate(Functions.REPLACE, "BBBB", "B", "\\\\")); + // See http://www.w3.org/TR/xquery-operators/#func-replace assertEquals("a*cada*", evaluate(Functions.REPLACE, "abracadabra", "bra", "*")); assertEquals("*", evaluate(Functions.REPLACE, "abracadabra", "a.*a", "*"));