Search code examples
regexwiremock

How to match a number between 1 to 12 via regular expression in wiremock bodyPatterns?


My request pattern looks like below

"request": {
        "url": "/stripe/v1/payment_methods",
        "method": "POST",
        "bodyPatterns": [
          {
            "matches": "type=card&card\\[cvc\\]=[0-9]{3}&card\\[number\\]=[1-9][0-9]{15}&card\\[exp_month\\]=[1-9]|1[0-2]&card\\[exp_year\\]=2[0-9][2-9][3-9]",
            "caseInsensitive": false
          }
        ]
      }

But exp_month is not matching and throwing the below error

-----------------------------------------------------------------------------------------------------------------------
| Closest stub                                             | Request                                                  |
-----------------------------------------------------------------------------------------------------------------------
                                                           |
v1_payment_methods                                         |
                                                           |
POST                                                       | POST
/stripe/v1/payment_methods                                 | /stripe/v1/payment_methods
                                                           | 
                                                           |
type=card&card\[cvc\]=[0-9]{3}&card\[number\]=[1-9][0-9]{  | type=card&card[cvc]=890&card[number]=4242424242424242&car<<<<< Body does not match
15}&card\[exp_month\]=[1-9]|1[0-2]&card\[exp_year\]=2[0-9  | d[exp_month]=1&card[exp_year]=2043
][2-9][3-9]                                                |
                                                           |
-----------------------------------------------------------------------------------------------------------------------

Can someone let me know how I can match a number between 1 to 12 via regular expression in bodyPatterns?


Solution

  • Use a capture group around the month values to limit the operation of the | to just those numbers

    &card\\[exp_month\\]=([1-9]|1[0-2])