# regex fails to match ^, $, \\b in multiline mode

**URL:** <https://forum.ansible.com/t/regex-fails-to-match-b-in-multiline-mode/35688>\
**Category:** Ansible Project\
**Created:** [August 30, 2021, 8:37am UTC](https://forum.ansible.com/t/regex-fails-to-match-b-in-multiline-mode/35688 "2021-08-30T08:37:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dennis\_M](https://avatars.discourse-cdn.com/v4/letter/d/34f0e0/32.png) [@Dennis\_M](https://forum.ansible.com/u/Dennis_M)\
**Post date:** [August 30, 2021, 8:37am UTC](https://forum.ansible.com/t/regex-fails-to-match-b-in-multiline-mode/35688/1 "2021-08-30T08:37:53Z")

</div>

Hi,  
I’m studying how to use regexp, and just follow documented examples,  
{{ ‘foo\nBAR’ | regex\_search(“^bar”, multiline=True, ignorecase=True) }}

expected result is: “bar”.  
Instead of “bar” it gets an empty string.  
Is it a bug, or do I miss an important setting?

Thanks,  
Dennis

---

<div class="post-metadata">

**Author:** ![Antony\_Stone](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Antony\_Stone](https://forum.ansible.com/u/Antony_Stone)\
**Post date:** [August 30, 2021, 8:50am UTC](https://forum.ansible.com/t/regex-fails-to-match-b-in-multiline-mode/35688/2 "2021-08-30T08:50:16Z")

</div>

See [https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_filters.html](https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html)  
#searching-strings-with-regular-expressions

and pay careful attention to details such as punctuation and case:

1. The example is:  
{{ 'foo\nBAR' | regex\_search('^bar', multiline=True, ignorecase=True) }}  
instead, you used:  
{{ 'foo\nBAR' | regex\_search("^bar", multiline=True, ignorecase=True) }}

2. The expected result is:  
# =\> 'BAR'  
whereas you say:  
expected result is: "bar".

Try exactly what the example shows, and let us know if it does not produce  
what the example should produce.

Antony.

---

<div class="post-metadata">

**Author:** ![Dennis\_M](https://avatars.discourse-cdn.com/v4/letter/d/34f0e0/32.png) [@Dennis\_M](https://forum.ansible.com/u/Dennis_M)\
**Post date:** [August 31, 2021, 6:43am UTC](https://forum.ansible.com/t/regex-fails-to-match-b-in-multiline-mode/35688/3 "2021-08-31T06:43:57Z")

</div>

Hi,

BTW, ansible-2.9.24-2.el7.

“bar” instead of “BAR” was just a mistype.  
Double quote instead of single quote plays no role, i.e. this YAML:

- hosts: monitor1p  
tasks:

- debug:  
msg: Debug\> {{ ‘foo\nBAR’ | regex\_search(‘^bar’, multiline=True, ignorecase=True) }}

produces same output:

ok: [monitor1p] =\> {  
“msg”: "Debug\> "  
}

Regards,

понедельник, 30 августа 2021 г. в 11:50:44 UTC+3, Antony Stone:

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [August 31, 2021, 7:43am UTC](https://forum.ansible.com/t/regex-fails-to-match-b-in-multiline-mode/35688/4 "2021-08-31T07:43:48Z")

</div>

You need to quote the entire jinja expression itself (including the  
curlies) if you use it as a yaml value.  
Because the single quotes are already using inside the expression,  
you;'d have to use the double quotes:

&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "Debug\> {{ 'foo\nBAR' | regex\_search('^bar',  
multiline=True, ignorecase=True) }}"
