<blockquote>
<p><strong>Important</strong></p>
<p>Since AWS 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.</p>
<p>For example, if you include a <code>\d</code> in your regular expression to match a digit character, you will need to write it as <code>\\d</code> in your JSON template.</p>
</blockquote>
<p><a href="http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-regexes.html" rel="nofollow noreferrer">http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-regexes.html</a></p>
<p>The very same applies to your <code>\s</code> expressions. Use <code>\\s</code> and it works.</p>
<p><strong>EDIT:</strong></p>
<p>Looking back the question I started to wonder the missing quotation marks on your definition. I found out the expessions are yaml format that is the second way to define things in aws.</p>
<p>If you should follow the yaml syntax, the right way to give string values is like this:</p>
<pre><code>AllowedPattern = '/[^\s@]+@[^\s@]+\.[^\s@]+/'
</code></pre>
<p><a href="https://aws.amazon.com/blogs/aws/aws-cloudformation-update-yaml-cross-stack-references-simplified-substitution/" rel="nofollow noreferrer">https://aws.amazon.com/blogs/aws/aws-cloudformation-update-yaml-cross-stack-references-simplified-substitution/</a></p>
<p><strong>EDIT:</strong></p>
<blockquote>
<p>Use quotes if your value includes special characters, (e.g. :, {, }, [, ], ,, &amp;, *, #, ?, |, -, &lt;, &gt;, =, !, %, @, ).</p>
</blockquote>
<p><a href="https://stackoverflow.com/questions/19109912/do-i-need-quotes-for-strings-in-yaml">https://stackoverflow.com/questions/19109912/do-i-need-quotes-for-strings-in-yaml</a></p>

CC BY-SA 4.0
2018-10-16T05:15:24.080Z
AWS documentation does not include starting `/` and trailing `/` slashes
Felipe Alvarez
CC BY-SA 3.0
2017-12-17T14:54:45.900Z
2017-12-18T17:30:15.197Z
2020-06-18T08:28:10.803Z
mico
<p>This worked for me in YAML format</p>
<pre><code>Parameters:
  MyEmail:
    Description: Email for notifications
    Type: String
    AllowedPattern: '[^@]+@[^@]+\.[^@]+'
</code></pre>

CC BY-SA 4.0
2022-01-28T17:31:49.417Z
2022-01-28T17:31:49.417Z
northben
<p>I am struggling with specifying a regular expression for email parameter of Cloudformation :</p>

<pre><code>Parameters:
  MyEmail :
    Type: String
    Description: Email for notifications
    AllowedPattern: /[^\s@]+@[^\s@]+\.[^\s@]+/
    ConstraintDescription: You should enter a valid email
</code></pre>

<p>As you see, I tried <code>/[^\s@]+@[^\s@]+\.[^\s@]+/</code> and it won't work. </p>

<p>What is the regular expression for emails in Cloudformation template ? </p>

<p><a href="/questions/tagged/aws" class="post-tag" title="show questions tagged &#39;aws&#39;" rel="tag">aws</a> <a href="/questions/tagged/regex" class="post-tag" title="show questions tagged &#39;regex&#39;" rel="tag">regex</a> <a href="/questions/tagged/aws-cloudformation" class="post-tag" title="show questions tagged &#39;aws-cloudformation&#39;" rel="tag">aws-cloudformation</a></p>

<amazon-web-services><infrastructure-as-code>
AllowedPattern for emails in Cloudformation template
Abdennour TOUMI
# AllowedPattern for emails in Cloudformation template

(Asked by: username_0 on 2017-12-16)

I am struggling with specifying a regular expression for email parameter of Cloudformation :
Parameters:
  MyEmail :
    Type: String
    Description: Email for notifications
    AllowedPattern: /[^\s@]+@[^\s@]+\.[^\s@]+/
    ConstraintDescription: You should enter a valid email

As you see, I tried /[^\s@]+@[^\s@]+\.[^\s@]+/ and it won't work. 
What is the regular expression for emails in Cloudformation template ? 
aws regex aws-cloudformation

**Tags:** amazon-web-services, infrastructure-as-code


---

## 2 answers

### Answer by username_1 on 2017-12-17. Score: 5

Important
Since AWS 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.

http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-regexes.html
The very same applies to your \s expressions. Use \\s and it works.
EDIT:
Looking back the question I started to wonder the missing quotation marks on your definition. I found out the expessions are yaml format that is the second way to define things in aws.
If you should follow the yaml syntax, the right way to give string values is like this:
AllowedPattern = '/[^\s@]+@[^\s@]+\.[^\s@]+/'

https://aws.amazon.com/blogs/aws/aws-cloudformation-update-yaml-cross-stack-references-simplified-substitution/
EDIT:

Use quotes if your value includes special characters, (e.g. :, {, }, [, ], ,, &, *, #, ?, |, -, <, >, =, !, %, @, ).

https://stackoverflow.com/questions/19109912/do-i-need-quotes-for-strings-in-yaml

> **Comments:**
> **username_2**: AWS documentation does not include starting `/` and trailing `/` slashes  

---

### Answer by username_3 on 2022-01-28. Score: 1

This worked for me in YAML format
Parameters:
  MyEmail:
    Description: Email for notifications
    Type: String
    AllowedPattern: '[^@]+@[^@]+\.[^@]+'


---

