I am receiving a date in message as a string. the following regular expression will confirm it is in at least the format I know I can handle:
^[0-9]{4}\-(0?[1-9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])$
but when I provide this regex to the validator.matchesRegex method in the Mule Expression language like so:
<when expression="#[validator.matchesRegex(payload.DateOfBirth,'^[0-9]{4}\-(0?[1-9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])$') == false]">
<set-variable variableName="validation_message" value="{"error": "Invalid DateOfBirth"}" doc:name="invalid DateOfBirth"/>
</when>
I receive the following error:
org.mule.api.MessagingException: [Error: illegal escape sequence: -]
[Near : {... eOfBirth,'^[0-9]{4}\-(0?[1-9]|1[012])\-(0?[1-9]|[1 ....}]
^
[Line: 1, Column: 55] (org.mule.api.expression.InvalidExpressionException). Message payload is of type: HashMap
update I've tried two new alterations:
doubling up my curly brackets like so:
^[0-9]{{4}}-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])$
but I get the same error but a few characters off:
[Error: illegal escape sequence: -]
[Near : {... fBirth,'^[0-9]{{4}}\-(0?[1-9]|1[012])\-(0?[1-9]|[1 ....}]
^
[Line: 1, Column: 57] (org.mule.api.expression.InvalidExpressionException). Message payload is of type: HashMap
unescaping the dashes between the numbers
'^[0-9]{{4}}-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])$
but I get the following error:
org.mule.api.MessagingException: Execution of the expression "validator.matchesRegex(payload.DateOfBirth,'^[0-9]{{4}}-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])$') == false" failed. (org.mule.api.expression.ExpressionRuntimeException). Message payload is of type: HashMap
I have changed the pattern to not use the escape backslash to only accept the dash:
^[0-9]{4}.(0?[1-9]|1[012]).(0?[1-9]|[12][0-9]|3[01])$ I have replaced '-' with '.' which is not really what I want, but it at least validates the number portions. I have confirmed this works. it just allows invalid values such as '2016!02_23' when it really should only allow '2016-02-23'
TL;DR: there is a bug with regular expressions within MEL when you are trying to escape the dash character?