2

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="{&quot;error&quot;: &quot;Invalid DateOfBirth&quot;}" 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:

  1. 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
  1. 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
  1. 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?

Nathan Tregillus
  • 6,006
  • 3
  • 52
  • 91

1 Answers1

1

Mule (as this page indicates) does not give a way to avoid the evil escaped escape. If you use a \ in your regex, you must escape it: \\. Java is the same way.

Also, you need to understand that certain regex symbols do not always need to be escaped. This is very important when you work in Mule/Java, because it means you avoid the evil escaped escape.

Depending on where they appear in the regex, characters may either gain or lose meaning as meta characters. The - character only has special meaning when it is sandwiched inside [] character classes. This means that you just can just use it normally instead of escaping it in your regular expression.

I suggest that you read up on regexes.

There will be times that you need to use the evil escaped escape, which can get confusing. Personally, I usually use this site to convert my regexes into escaped Strings.

Laurel
  • 5,965
  • 14
  • 31
  • 57