字串處理-${#},expr length,expr index,expr match,抽取子串

來源:互聯網
上載者:User

標籤:字串處理

######${#} ,  expr length "$str"[[email protected] shell]# str="my name is MAH"[[email protected] shell]# echo ${#str}14[[email protected] shell]# expr length $strexpr: syntax error[[email protected] shell]# expr length "$str"     #必須要加雙引號14########expr index "$str" MAH[[email protected] shell]# str="my name is MAH"[[email protected] shell]# expr index "$str" MAH12[[email protected] shell]# expr index "$str" o    #沒有出現,返回00[[email protected] shell]# expr index "$str" Ay    #儘管Ay都存在,但是返回的是y的位置2[[email protected] shell]# expr index "$str" an4

######expr match $string  $substring

在string的開頭,匹配substring字串,返回匹配的字串的長度

[[email protected] shell]# str="my name is MAH"[[email protected] shell]# expr match "$str" my*2[[email protected] shell]# expr match "$str" MAH     #儘管字串中包含MAH,但是MAH不在開頭0[[email protected] shell]# expr match "$str" m*1[[email protected] shell]# expr match "$str" m.*     #Regex .* 表示所有14
####${str:position:length}字串剪裁,兩種方式[[email protected] shell]# str="abc def ghi"[[email protected] shell]# echo ${str:0}abc def ghi[[email protected] shell]# echo ${str:0:3}    #0表示第一位,3表示長度abc[[email protected] shell]# echo ${str:0:5}abc d[[email protected] shell]# echo ${str:-3}    #-前面沒有空格,顯示所有字串abc def ghiYou have new mail in /var/spool/mail/root[[email protected] shell]# echo $strabc def ghi[[email protected] shell]# echo ${str: -3}   #-前有空格,從右邊剪裁ghi[[email protected] shell]# echo ${str: -6}ef ghi[[email protected] shell]# expr substr "$str" 1 3   #expr substr "$str"格式,首位是1號位,後面必須有兩個參數abc[[email protected] shell]# expr substr "$str" 1 5abc d

######字串處理時,使用Regex,注意:冒號兩邊必須有空格,否則語法錯誤

剪裁字串開頭的子串:兩種方式

[[email protected] shell]# str="012345 my name is mah"[[email protected] shell]# expr match "$str" ‘\([0-9]*\)‘    #方式一,使用match012345[[email protected] shell]# expr "$str" : ‘\([0-9]*\)‘       #方式二,使用冒號012345

剪裁字串結尾的子串:也是兩種方式區別是在\(前面添加.*

[[email protected] shell]# echo $str012345 my name is mah[[email protected] shell]# expr match "$str" ‘.*\(...\)‘   #.表示一個字元mah[[email protected] shell]# expr "$str" : ‘.*\(...\)‘mah


######刪除子串:格式一種:${}

第一類:從開頭開始刪除

[[email protected] shell]# echo $str012345 my name is mah[[email protected] shell]# echo ${str#0123}45 my name is mah[[email protected] shell]# echo ${str#345}012345 my name is mah[[email protected] shell]# echo ${str#0*5}   #這裡*不是Regex,僅僅代表從0到5中所有字元my name is mah[[email protected] shell]# echo ${str##0*5}  #  ##貪婪模式my name is mah

第二類:從結尾開始刪除

[[email protected] shell]# echo ${str%mah}012345 my name is[[email protected] shell]# echo ${str%%m*h}012345[[email protected] shell]# echo ${str%%m.*h}   #由此可見,*不是正則表示式012345 my name is mah

#######替換字串

[[email protected] shell]# str="0011 my name is mah 0011 "[[email protected] shell]# echo ${str/0*1/yyy}yyy[[email protected] shell]# echo ${str/0**/yyy}yyy[[email protected] shell]# echo ${str/0*0/yyy}  #*號不是Regex,但是表示的是所有的字元yyy11[[email protected] shell]# echo ${str//0*0/yyy}yyy11[[email protected] shell]# echo ${str/0*m/yyy}yyyah 0011另外兩種特殊的替換#1.從開頭替換[[email protected] shell]# echo ${str/#0011/MAH}MAH my name is mah 0011#2.從結尾替換[[email protected] shell]# echo ${str}0011 my name is mah 0011[[email protected] shell]# echo ${str/%0011/MAH}0011 my name is mah 0011[[email protected] shell]# str="123 my name is mah"[[email protected] shell]# echo ${str/%m*h/MAH}123 MAH

本文出自 “8176010” 部落格,謝絕轉載!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.