Darmach
Darmach

Reputation: 39

ansible string.split() with multiple delimiters

I'm having a hard time trying to split the string into list using multiple delimiters. I could just split it twice like follows:

myString.split(':')[1].split('.')

However this would look so unelegant. In my head, I'd like to do something like:

myString.split(': | .')

Is that even a thing?

Upvotes: 3

Views: 14563

Answers (1)

Kanwar Saad
Kanwar Saad

Reputation: 2337

I can think of two ways to do it.

Replace all delimeters with a single delimeter and then split:

- set_fact: my_list="{{ (myString | regex_replace(':|.', ':')).split(':') }}"

Split using regular expression:

- set_fact: mystr="{{ myString | regex_findall('([^:.]+)') }} "

Upvotes: 6

Related Questions