Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ansible string.split() with multiple delimiters

Tags:

split

ansible

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?

like image 873
Darmach Avatar asked Dec 22 '25 22:12

Darmach


1 Answers

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('([^:.]+)') }} "
like image 81
Kanwar Saad Avatar answered Dec 25 '25 20:12

Kanwar Saad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!