-1
"{{ archive_name | regex_replace('^(.*-)?.*-(.*)-.*-.*-.*-.*', '\\g<1>')}}"

Command above gives me an error in Ansible. archive_name is in the format of alpine-1.10-324bghz-i-2018-0503. I'm trying to grab the version number 1.10. It returns an error with \\g<1>\ instead of the version number. Am I doing something wrong? Any help or guidance is greatly appreciated here.

Thank you in advance!

Mihado
  • 1,487
  • 3
  • 17
  • 30
  • 1
    No sure but if `\\g<1>` points to the first capturing group then you might use the second capturing group as that is what the regex matches for your string [`^(.*-)?.*-(.*)-.*-.*-.*-.*`](https://regex101.com/r/lHyaSy/1). Your first capturing group is optional. Or update the regex to [`^.*-(.*)-.*-.*-.*-.*`](https://regex101.com/r/lHyaSy/2) – The fourth bird May 24 '18 at 06:41
  • No luck unfortunately, the output: \"hub.docker.com/centos:\\g<1>\" – Mihado May 24 '18 at 06:49
  • Did you try using [\\1](http://docs.ansible.com/ansible/latest/modules/replace_module.html#examples) instead of `\\g<1>`? – The fourth bird May 24 '18 at 06:53
  • Yes I did, I get the following: \"hub.docker.com/alpine:\\1\" – Mihado May 24 '18 at 07:01

1 Answers1

1

You could simply split the string, avoiding regex altogether.

{{ archive_name.split('-')[1:2] | join('-') }}

In theory the result should return only 1.10 (unable to test).

l'L'l
  • 44,951
  • 10
  • 95
  • 146