Search code examples
javaregexramlmulesoft

RegEx pattern for comma separated 10 currencies in URI parameter


I need to pass a list of comma separated currencies as URI parameter.

I need a regEx to allow only capital letters in group of three, separated by comma, and that does not allow any white space character.

I tried ((?=\S)[A-Z\s\,]){3,39}+$ regEx

It is working fine for input like: USD, ,J

But it start falling for input like : USD, ,JPY


Solution

  • Use:

    ^[A-Z]{3}(?:,[A-Z]{3}){0,9}$
    

    This will match 1 upto 10 currencies comma separated

    Explanation:

    [A-Z]{3}        # 3 letters
    (?:             # start non capture group
        ,           # a comma
        [A-Z]{3}    # 3 letters
    ){0,9}          # end group, may appear 0 upto 9 times