# Why doesn't Ansible like my regexp?

**URL:** <https://forum.ansible.com/t/why-doesnt-ansible-like-my-regexp/28944>\
**Category:** Ansible Project\
**Created:** [November 5, 2018, 5:13pm UTC](https://forum.ansible.com/t/why-doesnt-ansible-like-my-regexp/28944 "2018-11-05T17:13:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [November 5, 2018, 5:13pm UTC](https://forum.ansible.com/t/why-doesnt-ansible-like-my-regexp/28944/1 "2018-11-05T17:13:39Z")

</div>

`

As far as I can tell I have all of my quotes… it just doesn’t seem to like my escaping… Adding verbosity didn’t give me any useful information.

`

`

- name: Sanitize vm.cfg step 2 of  
lineinfile:  
path: “{{ file }}”  
regexp: “{{ item.regexp }}”  
line: “{{ item.line }}”  
backrefs: “{{ item.backrefs }}”  
with\_items:
- { regexp: “^(vif.\*‘mac=)((\w{2}:){3})((\w{2}:?){3})(.+)’$”, line: “\1\2AA:{{ 99 | random(start=10, step=1) }}:{{ 99 | random(start=10, step=1) }}\6”: backrefs: yes }
- { regexp: “(^uuid)(?:[=\s’]+)'(._)'._$”, line: “{{ newid.stdout }}”, backrefs: no }
- { regexp: “(^name)(?:[=\s’]+)'(._)'._$”, line: “{{ newid.stdout }}”, backrefs: no }

`

Result:

`  
ERROR! Syntax Error while loading YAML.  
found unknown escape character

The error appears to have been in ‘/etc/ansible/playbooks/one-offs/test.yml’: line 46, column 37, but may  
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

with\_items:

- { regexp: “^(vif.\*‘mac=)((\w{2}:){3})((\w{2}:?){3})(.+)’$”, line: “\1\2AA:{{ 99 | random(start=10, step=1) }}:{{ 99 | random(start=10, step=1) }}\6”: backrefs: yes }  
^ here  
We could be wrong, but this one looks like it might be an issue with  
missing quotes. Always quote template expression brackets when they  
start a value. For instance:

with\_items:

- {{ foo }}

Should be written as:

with\_items:

- “{{ foo }}”

`

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 5, 2018, 5:34pm UTC](https://forum.ansible.com/t/why-doesnt-ansible-like-my-regexp/28944/2 "2018-11-05T17:34:07Z")

</div>

In my experience this is because of double quotes around regexp and line that contains backslash, it is treated as a string and do expand escapes.  
You can avoid that by using single quotes instead, but then you need to escape the single quotes in you regexp.

What I do is avoid quotes all together, to do that you need to write your with\_items in another style

&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;- regexp: ^(vif.\*'mac=)((\w{2}:){3})((\w{2}:?){3})(.+)'$  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line: \1\2AA:{{ 99 | random(start=10, step=1) }}:{{ 99 | random(start=10, step=1) }}\6  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;backrefs: yes  
&nbsp;&nbsp;&nbsp;&nbsp;- regexp: (^uuid)(?:[=\s']+)'(.\*)'.\*$  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line: "{{ newid.stdout }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;backrefs: no  
&nbsp;&nbsp;&nbsp;&nbsp;- regexp: (^name)(?:[=\s']+)'(.\*)'.\*$  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line: "{{ newid.stdout }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;backrefs: no

This should work and the double quotes in the line must be there since { after colon in yaml is treated as a dict.

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [November 5, 2018, 5:38pm UTC](https://forum.ansible.com/t/why-doesnt-ansible-like-my-regexp/28944/3 "2018-11-05T17:38:26Z")

</div>

Thanks Kai! That was the issue. No complaints from Ansible now.
