RegExp for Redis Auth Token on AWS

Viewed 513

Looking for regular expression to use in a cloudformation script with below rules:

  • Must be only printable ASCII characters.
  • Must be at least 16 characters and no more than 128 characters in length.
  • Cannot contain any of the following characters: '/', '"', or '@'.

    "AllowedPattern": "[0-9a-zA-Z\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\[\\\]\^\_\{\|\}\~].{16,128}$",

Tried above but JSON doesnt seem to accept it. I believe some issue with \, as this "AllowedPattern": "[0-9a-zA-Z].{16,128}$", seems to work. Any help ?

1 Answers

You should remove . before the quantifier, or you will quantify the dot rather than the character class with the chars you want to allow.

Also, add ^ at the start to make sure the length restriction is applied at all.

Also, you may avoid issues with escaping style if you write the character ranges in a special way. You can write the pattern as

"^[0-9a-zA-Z#-.:-?\\[-_{-~]{16,128}$"

It will match 16 to 128 char strings that only contain

  • 0-9 - digits
  • a-zA-Z - ASCII letters
  • #-. - #, $, %, &, ', (, ), *, +, ,, -, .
  • :-? - :, ;, <, =, >, ?
  • \[-_ - \, ], ^, _
  • {-~ - {, |, }. ~

See the regex demo.

Related