# Trouble with regex\_replace

**URL:** <https://forum.ansible.com/t/trouble-with-regex-replace/23568>\
**Category:** Ansible Project\
**Created:** [January 26, 2017, 2:51pm UTC](https://forum.ansible.com/t/trouble-with-regex-replace/23568 "2017-01-26T14:51:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yuval\_Mund](https://avatars.discourse-cdn.com/v4/letter/y/f17d59/32.png) [@Yuval\_Mund](https://forum.ansible.com/u/Yuval_Mund)\
**Post date:** [January 26, 2017, 2:51pm UTC](https://forum.ansible.com/t/trouble-with-regex-replace/23568/1 "2017-01-26T14:51:55Z")

</div>

Hello,

I’m having trouble parsing a string.

The situation:

- parameters:

full: [‘hostname1:abcdefg1’,‘hostname2:-vip2’,‘hostname3:abc-vip3’]

It is required of me to have a parameter/list of hostnames without the rest of the string  
i.e  
just\_hostnames: [‘hostname1’,‘hostname2’,‘hostname3’]

I tried using using regex\_replace like this  
just\_hostnames: “{{ full | regex\_replace(‘:(?(?!’))\*', ‘’) }}”

But i got the error -  
template error while templating string expected token ‘end of print statement’, got ‘.’.

If you need further information about the usecase please ask!

Thank you very much!!

---

<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:** [January 27, 2017, 8:30am UTC](https://forum.ansible.com/t/trouble-with-regex-replace/23568/2 "2017-01-27T08:30:46Z")

</div>

Because full is a list you can't do it like this since regex\_replace don't work with lists.

Bu you can do something like this.

&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop\_item: "{{ item.split(':')[0] }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: loop\_result  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items: "{{ full }}"

&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;just\_hostnames: "{{ loop\_result.results | map(attribute='ansible\_facts.loop\_item') | list }}"
