我想使用bash shell来拆分字符串,比如:

Calcipotriol - Daivonex Cream 50mcg/1g 30 g [1]
Aspirin - DBL Aspirin 100mg [1] tablet

我想买“达维尼克斯霜”和“DBL阿司匹林”
我想在parttern***mg或***mcg或***g面前知道名字
怎么做?

最佳答案

在bash中,您可以执行以下操作:

while IFS= read -r line || [[ -n "$line" ]]; do
    if [[ "$line" =~ ^([[:alpha:]]+)[[:space:][:punct:]]+([[:alpha:][:space:]]+)[[:space:]](.*)$ ]]
    then
         printf "1:'%s' 2:'%s' 3:'%s'\n" "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}" "${BASH_REMATCH[3]}"
    fi
    done <<<"Calcipotriol - Daivonex Cream 50mcg/1g 30 g [1]
Aspirin - DBL Aspirin 100mg [1] tablet"

印刷品:
1:'Calcipotriol' 2:'Daivonex Cream' 3:'50mcg/1g 30 g [1]'
1:'Aspirin' 2:'DBL Aspirin' 3:'100mg [1] tablet'

关于regex - 我想在另一个字符串模式前面找到一些字符串,该怎么做?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42058416/

10-15 03:31