regex_replace('^p(\d+).*$', '\\1') '\\1' in the second argument is a "backref" (backwards reference) to the (\d+) in the first argument. It seems it is looking for an expression with digits and extracting the digits.
Your list 't' has names with p1_xyz, p2_xyz, p4_xyx so this regex would extract the 1, 2, 4 digits from those strings. Your string 's' has digits 1 and 2. You are getting two lines of output as expected. Walter -- Walter Rowe, Division Chief Infrastructure Services Division Mobile: 202.355.4123 On Jan 8, 2024, at 4:15 PM, rjwagn...@gmail.com <rjwagner....@gmail.com> wrote: Hi - Does anyone (who understands how backslashes work in Ansible/YAML) know why both of the following tasks work: (ansible2_15_8) rowagn@localhost:~#> cat d.yml - hosts: all gather_facts: no vars: s: 'This is a string containing 1 and 2.' t: - p1_xyz - p2_xyz - p4_xyz tasks: - name: single backslash debug: msg: '{{ item }} is in s' loop: '{{ t }}' when: ( item | regex_replace('^p(\d+).*$', '\\1') ) in s - name: double backslash debug: msg: '{{ item }} is in s' loop: '{{ t }}' when: ( item | regex_replace('^p(\\d+).*$', '\\1') ) in s (ansible2_15_8) rowagn@localhost:~#> ansible-playbook -i l d.yml PLAY [all] ****************************************************************************************************************************************************** TASK [single backslash] ***************************************************************************************************************************************** ok: [localhost] => (item=p1_xyz) => { "msg": "p1_xyz is in s" } ok: [localhost] => (item=p2_xyz) => { "msg": "p2_xyz is in s" } skipping: [localhost] => (item=p4_xyz) TASK [double backslash] ***************************************************************************************************************************************** ok: [localhost] => (item=p1_xyz) => { "msg": "p1_xyz is in s" } ok: [localhost] => (item=p2_xyz) => { "msg": "p2_xyz is in s" } skipping: [localhost] => (item=p4_xyz) PLAY RECAP ****************************************************************************************************************************************************** localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 The tasks are extracting the number from the strings in list t and then looking for that number in string s. What is strange is the second example at https://docs.ansible.com/ansible/latest/collections/ansible/builtin/regex_replace_filter.html#examples indicates the backslashes in both parameters need to be doubled, but the above testing shows double backslashes are not required in the first parameter (they are required in the second parameter). Thanks Rob -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscr...@googlegroups.com<mailto:ansible-project+unsubscr...@googlegroups.com>. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/bd36f3ef-9b36-4af1-aede-9435535d9fb0n%40googlegroups.com<https://groups.google.com/d/msgid/ansible-project/bd36f3ef-9b36-4af1-aede-9435535d9fb0n%40googlegroups.com?utm_medium=email&utm_source=footer>. -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CEE7C868-60EE-4D65-9303-878D705B3CE0%40nist.gov.