Using regular expressions in Amazon CloudFormation templates - Amazon CloudFormation
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Using regular expressions in Amazon CloudFormation templates

Regular expressions (commonly known as regexes) can be specified in a number of places within an Amazon CloudFormation template, such as for the AllowedPattern property when creating a template parameter.

Regular expressions in CloudFormation conform to the Java regular expression syntax. A full description of this syntax and its constructs can be viewed in the Java documentation, here: java.util.regex.Pattern.

Important

Because CloudFormation templates use the JSON syntax for specifying objects and data, you will need to add an additional backslash to any backslash characters in your regular expression, or JSON will interpret these as escape characters.

For example, if you include a \d in your regular expression to match a digit character, you will need to write it as \\d in your JSON template.