regex_replace unexpected results

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

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

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’)}}”