# can we fetch a regex match from a variable value

**URL:** <https://forum.ansible.com/t/can-we-fetch-a-regex-match-from-a-variable-value/18377>\
**Category:** Ansible Project\
**Created:** [June 2, 2015, 7:23am UTC](https://forum.ansible.com/t/can-we-fetch-a-regex-match-from-a-variable-value/18377 "2015-06-02T07:23:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Max1](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@Max1](https://forum.ansible.com/u/Max1)\
**Post date:** [June 2, 2015, 7:23am UTC](https://forum.ansible.com/t/can-we-fetch-a-regex-match-from-a-variable-value/18377/1 "2015-06-02T07:23:03Z")

</div>

Suppose I’ve url=[http://example.com:90/url](http://example.com:90/url) and now in my ansible task I want to get the url variable by {{ url }} and then do a regex match and fetch oly the hostname i.e. [example.com](http://example.com).  
Any directions on how this can be achieved would help.

---

<div class="post-metadata">

**Author:** ![user64](https://avatars.discourse-cdn.com/v4/letter/u/dec6dc/32.png) [@user64](https://forum.ansible.com/u/user64)\
**Post date:** [June 2, 2015, 8:22am UTC](https://forum.ansible.com/t/can-we-fetch-a-regex-match-from-a-variable-value/18377/2 "2015-06-02T08:22:14Z")

</div>

`

- name: Debug  
debug: msg=‘facter\_fqdn is {{ facter\_fqdn }}’  
register: ‘debug\_result’
- name: Fail  
fail: msg=‘Sample warning when fqnd != fqdn’  
when: facter\_fqdn.find == “{{ facter\_fqdn }}”
- name: Fail  
fail: msg=‘Sample warning when fqdn %like% [example.com](http://example.com)’  
when: facter\_fqdn.find(‘[example.com](http://example.com)’) != -1
- name: Fail  
fail: msg=‘Sample warning when fqdn %like% localdomain’  
when: facter\_fqdn.find(‘.local’) != -1

`

---

<div class="post-metadata">

**Author:** ![Max1](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@Max1](https://forum.ansible.com/u/Max1)\
**Post date:** [June 2, 2015, 8:27am UTC](https://forum.ansible.com/t/can-we-fetch-a-regex-match-from-a-variable-value/18377/3 "2015-06-02T08:27:22Z")

</div>

Thanks for the response.  
The goal is to match a regular expression and then use the value matched for doing other things. Just testing whether a particular string is present or not doesn’t suffice for me.  
[http://example.com:90/xyz](http://example.com:90/xyz)  
[http://dual.com.87/yjhh](http://dual.com.87/yjhh)  
My script should fetch me the host name by using a regex from any of the urls in the format specified above.

---

<div class="post-metadata">

**Author:** ![user64](https://avatars.discourse-cdn.com/v4/letter/u/dec6dc/32.png) [@user64](https://forum.ansible.com/u/user64)\
**Post date:** [June 2, 2015, 8:37am UTC](https://forum.ansible.com/t/can-we-fetch-a-regex-match-from-a-variable-value/18377/4 "2015-06-02T08:37:47Z")

</div>

Read down of [page](https://docs.ansible.com/playbooks_filters.html#other-useful-filters).
