# regex\_replace unexpected results

**URL:** <https://forum.ansible.com/t/regex-replace-unexpected-results/19501>\
**Category:** Ansible Developer\
**Created:** [October 14, 2015, 5:00am UTC](https://forum.ansible.com/t/regex-replace-unexpected-results/19501 "2015-10-14T05:00:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steve\_Kieu](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@Steve\_Kieu](https://forum.ansible.com/u/Steve_Kieu)\
**Post date:** [October 14, 2015, 5:00am UTC](https://forum.ansible.com/t/regex-replace-unexpected-results/19501/1 "2015-10-14T05:00:48Z")

</div>

I have these lines in a role/default/main.yml

# takes everything (including the -) from the second dash counted right to left

# Example if host is t5-s2-webapi-au the prefix domain is t5-s2, if host is staging1-web-au then it will be staging1

PREFIX\_DOMAIN: “{{inventory\_hostname\_short|regex\_replace(‘([^\s]+)[\-][^\-]+[\-][^\-]+$’,‘\\1’)}}”

NODE\_NUMBER: “{{PREFIX\_DOMAIN|regex\_replace(‘[^\d]+([\d]+)$’, ‘\\1’)}}”

Now I have the hostname is staging1-s2-web-au. From teh debug message I saw:

TASK: [web\_base | debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
\<10.10.7.37\> ESTABLISH CONNECTION FOR USER: root on PORT 22 TO 10.10.7.37  
ok: [staging1-s2-web-au] =\> {  
“msg”: “Starting deploy with IS\_MASTER false - PREFIX\_DOMAIN: staging1-s2 NODE\_NUMBER staging12”  
}

The PREFIX\_DOMAIN looks correct. But I expect the NODE\_NUMBER to be 2 rather than staging12.

Can someone please help me to see what is wrong with my regex or it is indeed a bug?

Thanks

---

<div class="post-metadata">

**Author:** ![Steve\_Kieu](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@Steve\_Kieu](https://forum.ansible.com/u/Steve_Kieu)\
**Post date:** [October 14, 2015, 5:04am UTC](https://forum.ansible.com/t/regex-replace-unexpected-results/19501/2 "2015-10-14T05:04:55Z")

</div>

From ipython I can see it is correct

In [1]: m = re.search(‘[^\d]+([\d]+)$’, ‘staging1-s2’)

In [2]: m.group(1)  
Out[2]: ‘2’

why not regex\_replace ? from the doc it says it uses python regex

---

<div class="post-metadata">

**Author:** ![Steve\_Kieu](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@Steve\_Kieu](https://forum.ansible.com/u/Steve_Kieu)\
**Post date:** [October 14, 2015, 5:06am UTC](https://forum.ansible.com/t/regex-replace-unexpected-results/19501/3 "2015-10-14T05:06:36Z")

</div>

It looks like regex\_replace use full string match so if I change like this it works

NODE\_NUMBER: “{{PREFIX\_DOMAIN|regex\_replace(‘^.\*[^\d]+([\d]+)$’, ‘\\1’)}}”
