Suppose if I wanted to extract the suffix string “aabdcd” from the string daaaaab*aabdcd* , the following expression is used in KSH,
${ex##+([!b])+([b])}
This means match and remove any character other than ‘b’ greedily followed by the character ‘b’. What I don’t understand is the + (highlighted in bold) preceding. Why is it required? Without it the regex fails to do the intended job.
#ex=daaaaabaabdcd
#echo ${ex##([!b])+([b])}
daaaaabaabdcd
#echo ${ex##+([!b])+([b])}
aabdcd
The + represents the regex quantifier of the result. So with the + you may receive multiple results. Here are the other quantifiers that can be used: