Search code examples
regexdatejenkinsdate-format

What's wrong with my MM/DD/YYYY regex in JENKINS


I need to validate the date format in MM/DD/YYYY. Null is a valid too in my scenario. This is my regex [0-9]{2}\/[0-9]{2}\/[0-9]{4}$ |

Below image shows the job configuration with my regex

Jenkins config

ERROR

Job error


Solution

  • You have space after the $ sign, that's why you input doesn't match.

    [0-9]{2}\/[0-9]{2}\/[0-9]{4}$ |
    //                    here __^
    

    Remove it ([0-9]{2}\/[0-9]{2}\/[0-9]{4}$) and, if you want to accept empty string, add empty string with a group, and add beginning of string anchor:

    ^([0-9]{2}\/[0-9]{2}\/[0-9]{4}|)$
    

    or, better, make the group optional

    ^([0-9]{2}\/[0-9]{2}\/[0-9]{4})?$
    

    Demo & explanation