Skip to content

Commit

Permalink
Support for replace with regexp
Browse files Browse the repository at this point in the history
This PR addresses issues schibsted#346 and schibsted#347.
Added support for Regexp pattern "Predefined character classes".
  • Loading branch information
Carlo Dapor committed May 26, 2024
1 parent d84fa16 commit 087a2df
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,13 @@ private static String makeString(ParseContext ctx, Token literal) {
result[pos++] = ch;
else {
ch = string.charAt(++ix);

// special Regexp characters, s. https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html,
// "Predefined character classes".
if ("dDhHsSvVwW".contains(String.valueOf(ch))) {
result[pos++] = '\\';
result[pos++] = ch;
continue;
}
switch (ch) {
case '\\': result[pos++] = ch; break;
case '"': result[pos++] = ch; break;
Expand Down
9 changes: 7 additions & 2 deletions core/src/test/resources/function-tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -1128,15 +1128,20 @@
"output": "\"\""
},
{
"query": "replace-regexp(., \"([0-9][0-9][0-9][0-9])-([0-9][0-9])-([0-9][0-9])\", \"$2/$3/$1\")",
"query": "replace-regexp(., \"(\\d{4})-(\\d{2})-(\\d{2})\", \"$2/$3/$1\")",
"input" : "\"2019-12-31\"",
"output": "\"12/31/2019\""
},
{
"query": "replace-regexp(., \"(?<year>[0-9][0-9][0-9][0-9])-(?<month>[0-9][0-9])-(?<day>[0-9][0-9])\", \"${day}.${month}.${year}\")",
"query": "replace-regexp(., \"(?<year>\\d{4})-(?<month>\\d{2})-(?<day>\\d{2})\", \"${day}.${month}.${year}\")",
"input" : "\"2019-12-31\"",
"output": "\"31.12.2019\""
},
{
"query": "replace-regexp(., \"([a-z]+)\", \"$1\")",
"input" : "\"2019-12-31\"",
"output" : "\"2019-12-31\""
},
{
"query": "trim(.)",
"input" : "\"some text\"",
Expand Down
1 change: 0 additions & 1 deletion core/src/test/resources/json-parse-error-tests.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"description" : "Tests that should cause the JSLT parser to declare JSON syntax error.",
"tests" : [
"\" \\d \"",
"\"\\u\"",
"\"\\u0\"",
"\"\\u00\"",
Expand Down
6 changes: 3 additions & 3 deletions functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -523,10 +523,10 @@ If the `regexp` does not match the input,`out` corresponds to `value`.
Examples:

```
replace-regexp("2019-12-31", "([0-9][0-9][0-9][0-9])-([0-9][0-9])-([0-9][0-9])", "$2/$3/$1")
replace-regexp("2019-12-31", "(\\d{4})-(\\d{2})-(\\d{2})", "$2/$3/$1")
=> "12/31/2019"
replace-regexp("2019-12-31", "(?<year>[0-9][0-9][0-9][0-9])-(?<month>[0-9][0-9])-(?<day>[0-9][0-9])",
=> "${day}.${month}.${year}") => "31.12.2019"
replace-regexp("2019-12-31", "(?<year>\\d{4})-(?<month>\\d{2})-(?<day>\\d{2})", "${day}.${month}.${year}")
=> "31.12.2019"
replace-regexp("2019-12-31", "([a-z]+)", "$1")
=> "2019-12-31"
```
Expand Down

0 comments on commit 087a2df

Please sign in to comment.