使用多字符定界符将字符串拆分为数组 [英] Split string into array using multi-character delimiter
本文介绍了使用多字符定界符将字符串拆分为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将字符串拆分为数组.我的问题是分隔符是一个3个字符的分隔符: _-_
例如:
I need to split a string into an array. My problem is that the delimiter is a 3 character one: _-_
For example:
db2-111_-_oracle12cR1RAC_-_mariadb101
我需要创建以下数组:
db2-111
oracle12cR1RAC
mariadb101
类似的问题也遵循这种方法:
Similar questions followed this approach:
str="db2-111_-_oracle12cR1RAC_-_mariadb101"
arr=(${str//_-_/ })
echo ${arr[@]}
即使创建了数组,也无法正确地对其进行分割:
Even if the array is created, it has been split uncorrectly:
db2
111
oracle12cR1RAC
mariadb101
似乎第一项中的-"字符导致数组的split函数失败.您可以提出解决方案吗?谢谢
It seems that the "-" character in the first item causes the array's split function to fail. Can you suggest a fix for it? Thanks
推荐答案
如果可以,请用另一个单字符替换 _-_
序列字段拆分.例如,
If you can, replace the _-_
sequences with another single character that you can use for field splitting. For example,
$ str="db2-111_-_oracle12cR1RAC_-_mariadb101"
$ str2=${str//_-_/#}
$ IFS="#" read -ra arr <<< "$str2"
$ printf '%s\n' "${arr[@]}"
db2-111
oracle12cR1RAC
mariadb101
这篇关于使用多字符定界符将字符串拆分为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文