Regex pattern for selecting an inverted word on a list of strings

18 views
Skip to first unread message

jean-christophe manciot

unread,
Aug 6, 2019, 8:57:18 AM8/6/19
to Ansible Project
The goal is to select all the lines of a list which do NOT contain a specific word.

For instance, "{{ files }}" contains a list of filenames. We need to select all the filenames which do not include the word 'all'.

So with:
files:
- aaaaaaaaaaa
- bbbbbbbbbbb
- cccccccccccc
- d-all-ddddddd

The expected result would be:
files_filtered::
- aaaaaaaaaaa
- bbbbbbbbbbb
- cccccccccccc

I unsuccessfully tried:
  set_fact:
        files_filtered: "{{ files | select('match', '^(.*^((?!all).)*.+)$') | list }}"

The result is empty.

Any suggestion?

Kai Stian Olstad

unread,
Aug 6, 2019, 9:13:10 AM8/6/19
to ansible...@googlegroups.com
On 06.08.2019 14:57, jean-christophe manciot wrote:
> The goal is to* select all the lines of a list which do NOT contain a
> specific word*.
>
> For instance, "{{ files }}" contains a list of filenames. We need to
> select
> all the filenames which do not include the word 'all'.
>
> So with:
> files:
> - aaaaaaaaaaa
> - bbbbbbbbbbb
> - cccccccccccc
> - d-all-ddddddd
>
> The expected result would be:
> files_filtered::
> - aaaaaaaaaaa
> - bbbbbbbbbbb
> - cccccccccccc
>
> I unsuccessfully tried:
> set_fact:
> files_filtered: "{{ files | select('match',
> '^(.*^((?!all).)*.+)$') | list }}"
>
> The result is empty.
>
> Any suggestion?

Use reject instead of select, and since you looking for containing use
search instead of match.

{{ files | reject('search', 'all') | list }}


--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages