# how to compare lists whose elements are regular expressions in Ansible?

**URL:** <https://forum.ansible.com/t/how-to-compare-lists-whose-elements-are-regular-expressions-in-ansible/33591>\
**Category:** Ansible Project\
**Created:** [August 12, 2020, 4:29am UTC](https://forum.ansible.com/t/how-to-compare-lists-whose-elements-are-regular-expressions-in-ansible/33591 "2020-08-12T04:29:40Z")\
**Posts on this page:** 3\
**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:** [August 12, 2020, 4:29am UTC](https://forum.ansible.com/t/how-to-compare-lists-whose-elements-are-regular-expressions-in-ansible/33591/1 "2020-08-12T04:29:40Z")

</div>

Hi Team

I wish to compare two lists, one list has regex expression and the second has ordinary data and return a match. Please help

e.g.

```auto

list1:
   - linux.*
   - 6.*
   - mysql|python|gvim

list2:
   - linux
   - 6.0.1
   - mysql

```

```auto
Thanks

```

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [August 12, 2020, 5:34am UTC](https://forum.ansible.com/t/how-to-compare-lists-whose-elements-are-regular-expressions-in-ansible/33591/2 "2020-08-12T05:34:50Z")

</div>

See "Testing strings" and chose from “match”, “search” or “regex”.  
[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_tests.html#testing-strings](https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html#testing-strings)

For example, the task below "match" items of the lists

&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ item.0 is match(item.1) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ list2|zip(list1)|list }}"

If "compare two lists" means you want to know whether all items match  
or not, create new list and test all items in it at once. For example  
[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_tests.html#test-if-a-list-contains-a-value](https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html#test-if-a-list-contains-a-value)

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list3: "{{ list3|default() +  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[item.0 is match(item.1)] }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ list2|zip(list1)|list }}"  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ list3 is all }}"

---

<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:** [August 12, 2020, 9:52am UTC](https://forum.ansible.com/t/how-to-compare-lists-whose-elements-are-regular-expressions-in-ansible/33591/3 "2020-08-12T09:52:43Z")

</div>

Thanks for your response,

I have still issue when there are two elements matched in regex pattern. please help

Playbook:
