2014-02-14 55 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$