2014-02-14 56 views
3

下面的正则表达式是什么意思?正则表达式的解释

fspec="/exp/home1/abc.txt" 
fname="${fspec##*/}" 

我知道它做了什么,但不知道它是怎么做的?获取fname对我来说并不清楚。

请解释。

+0

这不是一个正则表达式。 –

+0

@TimPietzcker,不,但提问者可以原谅它认为它是一个。关于shruti的参考,可以在[字符串操作](http://tldp.org/LDP/abs/html/string-manipulation.html)上的bash手册页中找到解释。 – cmh

回答

4

${var##*/}语法将所有内容删除到最后/

$ fspec="/exp/home1/abc.txt" 
$ echo "${fspec##*/}" 
abc.txt 

一般而言,${string##substring}$substring$string前面最长匹配。

有关进一步的参考,你可以例如检查Bash String Manipulation几个解释和例子。

1

下面是来自bash文档的解释。

${parameter#word} 
${parameter##word} 
The word is expanded to produce a pattern just as in pathname 
expansion. If the pattern matches the beginning of the value of 
parameter, then the result of the expansion is the expanded value 
of parameter with the shortest matching pattern (the ``#'' case) or 
the longest matching pattern (the ``##'' case) deleted. 

则根据上面的说明中,在您的例子字= */这意味着零(或)任意数量的/结束字符。

bash-3.2$fspec="/exp/home1/abc.txt" 
bash-3.2$echo "${fspec##*/}" # Here it deletes the longest matching pattern 
# (i.e) /exp/home1/ 
# Output is abc.txt 

bash-3.2$echo "${fspec#*/}" # Here it deletes the shortest matching patter 
#(i.e)/
# Output is exp/home1/abc.txt 
bash-3.2$