2

I have this snipplet:

---
- hosts: all
  gather_facts: False
  become: yes
  become_user: somesu
  become_method: sudo

  tasks:

  - set_fact:
      tmped: "{{varput | regex_search('(^(?:[^.]*.){4}([^.]*))')}}"

  - debug: msg="{{ tmped }}"

...

varput is

TEST123TEST.4.TES22TES.ZTER012UZT.20190910.555

and I want the debug to print out

20190910

However my regex print out

TEST123TEST.4.TES22TES.ZTER012UZT.20190910

any idea how to fix this?

Thanks

arenginsm na
  • 141
  • 1
  • 8

2 Answers2

2

Use split

tmped: "{{ varput.split('.')[-2] }}"
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
  • While this code may answer the question, providing additional context regarding *why* and/or *how* this code answers the question improves its long-term value. – Sneftel Sep 10 '19 at 13:00
  • The string is 1) split to an array and 2) last but one element of the array is selected. – Vladimir Botka Sep 10 '19 at 13:06
1

You already captured the necessary text into Group 1.

You can pass the second capturing group ID argument to regex_search to return the desired capturing group value:

tmped: "{{varput | regex_search('^(?:[^.]*\\.){4}([^.]*)','\\1') }}"

See the ansible source code.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563