> For the complete documentation index, see [llms.txt](https://kanneg881.gitbook.io/workspace/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kanneg881.gitbook.io/workspace/zhong-duan/bash-jiao-ben/zheng-gui-biao-shi-shi.md).

# 正規表示式

介紹一些比較常用的寫法

`${VAR%$'\r'}`\
% 運算子把字元從字串右側移除\
這邊舉例移除歸位字元 \r\
因為 \ 反斜線會逸出\
所以使用 $'\r' 轉換成 ASCII 碼

`${VAR%% *}`\
移除從右至左搜尋，直到找到 %% 後面的正規表示式的最長字串\
例如上面的寫法 'a b c d e' 會移除 ' b c d e' 得 'a'

`${VAR#*|}`\
從頭開始找到 # 後面最短的符合的正規表示式，並把它移除\
例如上面的寫法 'a | b | c' 會移除 'a |' 得 ' b | c'

`${VAR:0:1}`\
從位置0開始取出長度1的字元\
例如：'abc' 得 'a'

`${VAR/old/new}`\
把 VAR 中 old 字串替換成 new

`${VAR//old/new}`\
同上，但會把有找到的所有字串都替換
