2012-08-29 78 views
1

假设如果我想提取后缀字符串“aabdcd”从字符串daaaaab * aabdcd *,下面的表达式在KSH使用,为什么+中的regexp需要ksh的可变图案操纵

$ {EX ## +([!b])+([b])}

这意味着匹配并取出比 'b' 贪婪地随后的字符 'b' 以外的任何字符。我不明白的是前面的+(粗体突出显示)。为什么需要?没有它,正则表达式不能完成预期的工作。

#ex=daaaaabaabdcd 
#echo ${ex##([!b])+([b])} 
daaaaabaabdcd 
#echo ${ex##+([!b])+([b])} 
aabdcd 

回答

2

+表示结果的regex quantifier。因此,您可能会收到多个结果。以下是可以使用的其他量词:

?(pattern) matches zero or one times the pattern. 
*(pattern) matches any time the pattern. 
+(pattern) matches one or more time the pattern. 
@(pattern) matches one time the pattern. 
!(pattern) matches string without the pattern. 
+0

谢谢,那个链接让我意识到正则表达式量词用在Ksh的左边。这让我感到困惑,因为它们通常用在像java这样的语言中。 – toddlermenot