# searching multi lines using regex

**URL:** <https://forum.ansible.com/t/searching-multi-lines-using-regex/33860>\
**Category:** Ansible Developer\
**Created:** [September 21, 2020, 3:28pm UTC](https://forum.ansible.com/t/searching-multi-lines-using-regex/33860 "2020-09-21T15:28:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pushparaj\_Gothandam](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/pushparaj_gothandam/32/5538_2.png) [@Pushparaj\_Gothandam](https://forum.ansible.com/u/Pushparaj_Gothandam)\
**Post date:** [September 21, 2020, 3:28pm UTC](https://forum.ansible.com/t/searching-multi-lines-using-regex/33860/1 "2020-09-21T15:28:47Z")

</div>

How to search multiple lines using regex in Ansible?

I tried below pattern and it is not working

regex: ^line con._(?:\n^\s._)

line console 0  
password

---

<div class="post-metadata">

**Author:** ![dnsbob](https://avatars.discourse-cdn.com/v4/letter/d/ea5d25/32.png) [@dnsbob](https://forum.ansible.com/u/dnsbob)\
**Post date:** [September 21, 2020, 3:39pm UTC](https://forum.ansible.com/t/searching-multi-lines-using-regex/33860/2 "2020-09-21T15:39:10Z")

</div>

I don’t think you can use caret " ^ " in the middle of a regex. It is probably trying to match a literal ^ symbol and not finding it. Try removing the caret.

---

<div class="post-metadata">

**Author:** ![Pushparaj\_Gothandam](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/pushparaj_gothandam/32/5538_2.png) [@Pushparaj\_Gothandam](https://forum.ansible.com/u/Pushparaj_Gothandam)\
**Post date:** [September 21, 2020, 4:01pm UTC](https://forum.ansible.com/t/searching-multi-lines-using-regex/33860/3 "2020-09-21T16:01:56Z")

</div>

Hi Bob,

here my code I removed ‘^’ still not working

- set\_fact:  
console: “{{ current\_show\_run[0] | map(‘regex\_search’, qry, multiline=True | select(‘string’) | list }}”  
vars:  
qry: ‘^line con.\*\n\spassword’

---

<div class="post-metadata">

**Author:** ![Abhijit\_Menon-Sen1](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@Abhijit\_Menon-Sen1](https://forum.ansible.com/u/Abhijit_Menon-Sen1)\
**Post date:** [September 28, 2020, 5:22am UTC](https://forum.ansible.com/t/searching-multi-lines-using-regex/33860/4 "2020-09-28T05:22:39Z")

</div>

This list is for discussions about Ansible development. The Ansible  
Project mailing list would be the right forum for your question. See  
[https://docs.ansible.com/ansible/latest/community/communication.html](https://docs.ansible.com/ansible/latest/community/communication.html)  
for details.

The approach you are using works as expected in this test (caret included):

```auto
$ ansible localhost -m debug -a msg="{{ lines|map('regex_search',
'^line con.*\n^\s(.*)', multiline=True)|select('string')|list }}" -e
'{"lines":["foo\nbar","line console 0\n password","mine console
1\npassword","line console 2\nnospaceword","line console 3\n
otherpassword"]}'
localhost | SUCCESS => {
   "msg": [
       "line console 0\n password",
       "line console 3\n otherpassword"
   ]
}

```

Either the input data you're feeding it isn't what you expect, or your  
pattern doesn't match the correct input.

-- Abhijit
