Using Smooks with Freemarker to transform CSV to JSON. While adding a regular expression using matches in Freemarker the syntax fails and get the following exceptions. When that particular condition is removed it all works fine.

Regular expression is to check if the string is a proper exponent.

I've tried the same regex in Java and it works fine.

<#elseif message?contains("E+") && message?matches("[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?")>

Exceptions:

org.milyn.cdr.SmooksConfigurationException: 

Error invoking @Initialize method 'initialize' on class 'org.milyn.templating.freemarker.FreeMarkerTemplateProcessor'.`

org.milyn.cdr.SmooksConfigurationException:

Error loading Templating resource: Target Profile: [[org.milyn.profile.Profile#default_profile]], Selector:`

1 Answers

1
ddekany On

It fails as \. is not a valid escape in a string literal. (For the same reason compilation would fail in Java as well.) You either have to write \\. instead (just like in Java), or better, start the string literal with an r and keep the \. as is (r for raw string literal, which doesn't treat \ specially): r"[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?"

BTW, if that's really all that Smooks shows, it's not very helpful. The parsing exception should be there somewhere... but if not, you can use https://try.freemarker.apache.org/ to try your template snippets.