regex_replace

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?

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

ok