Skip to content

Commit

Permalink
Fixed the evaluation of 'replace' built-in function
Browse files Browse the repository at this point in the history
  • Loading branch information
vruusmann committed Nov 21, 2024
1 parent d759022 commit 3fabeb7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
20 changes: 20 additions & 0 deletions pmml-evaluator/src/test/java/org/jpmml/evaluator/FunctionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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", "*"));
Expand Down

0 comments on commit 3fabeb7

Please sign in to comment.