# regex\_replace

**URL:** <https://forum.ansible.com/t/regex-replace/30105>\
**Category:** Ansible Project\
**Created:** [April 12, 2019, 3:00pm UTC](https://forum.ansible.com/t/regex-replace/30105 "2019-04-12T15:00:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luca\_Cazzaniga](https://avatars.discourse-cdn.com/v4/letter/l/977dab/32.png) [@Luca\_Cazzaniga](https://forum.ansible.com/u/Luca_Cazzaniga)\
**Post date:** [April 12, 2019, 3:00pm UTC](https://forum.ansible.com/t/regex-replace/30105/1 "2019-04-12T15:00:29Z")

</div>

hi all I don’t get why the following regexp replacement matches

vars:  
monit\_pkgname: “prefixmonit-5.25.3-1”

tasks:

- name: debug  
debug:  
msg: “{{ monit\_pkgname\_release }}”

vars:

monit\_pkgname\_release: “{{ (monit\_pkgname|regex\_replace(‘(?\<!prefix)monit-[0-9]+(?:\.[0-9]+)\*-([0-9]+)’, ‘\1’)).split(‘.’) }}”

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [capsule] =\> {  
“msg”: “[u’finecomonit-5’, u’25’, u’3-1’]”  
}

from regexp manual

`(?\<!...)`Matches if the current position in the string is not preceded by a match for `...`. This is called a \*negative lookbehind assertion\*.

The same happens with caret

vars:

monit\_pkgname\_version: “{{ (monit\_pkgname|regex\_replace(‘^monit-([0-9]+(?:\.[0-9]+)\*)-[0-9]+’, ‘\1’)).split(‘.’) }}”

The aim would be made the replacement not happen if there’s a prefix in monit\_pkgname-  
any idea?

---

<div class="post-metadata">

**Author:** ![Luca\_Cazzaniga](https://avatars.discourse-cdn.com/v4/letter/l/977dab/32.png) [@Luca\_Cazzaniga](https://forum.ansible.com/u/Luca_Cazzaniga)\
**Post date:** [April 12, 2019, 3:21pm UTC](https://forum.ansible.com/t/regex-replace/30105/2 "2019-04-12T15:21:27Z")

</div>

Solved

monit\_pkgname\_version: “{{ (monit\_pkgname|regex\_replace(‘monit-([0-9]+(?:\.[0-9]+)_)-[0-9]+‘, ‘\1’)).split(’.‘) if monit\_pkgname|regex\_search(’^monit-') else None }}"  
monit\_pkgname\_release: "{{ (monit\_pkgname|regex\_replace('monit-[0-9]+(?:\.[0-9]+)_-([0-9]+)’, ‘\1’)).split(‘.’) if monit\_pkgname|regex\_search(‘^monit-’) else None }}”

---

<div class="post-metadata">

**Author:** ![Raghavendra\_Rao](https://avatars.discourse-cdn.com/v4/letter/r/c77e96/32.png) [@Raghavendra\_Rao](https://forum.ansible.com/u/Raghavendra_Rao)\
**Post date:** [April 12, 2019, 3:29pm UTC](https://forum.ansible.com/t/regex-replace/30105/3 "2019-04-12T15:29:41Z")

</div>

ok
