Unix字符串拆分问题

时间:2012-11-22 09:10:29

标签: shell unix

在搜索unix问题时,我遇到了以下字符串拆分正则表达式语句:

 string="abc@hotmail.com;xyz@gmail.com;uvw@yahoo.com"
    str1=${string%%;*} 
    str2=${string##*;}

    Result:
    str1=abc@hotmail.com 
    str2=uvw@yahoo.com

我不明白第二个表达式(str2)。 可以请别人帮忙。

由于

1 个答案:

答案 0 :(得分:1)

bash手册的Parameter Expansion下:

   ${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 pat-
          tern (the ``#'' case) or the longest matching pattern (the ``##'' case) deleted.  If parameter is @  or  *,  the  pattern
          removal operation is applied to each positional parameter in turn, and the expansion is the resultant list.  If parameter
          is an array variable subscripted with @ or *, the pattern removal operation is applied to each member  of  the  array  in
          turn, and the expansion is the resultant list.

   ${parameter%word}
   ${parameter%%word}
          The  word  is  expanded to produce a pattern just as in pathname expansion.  If the pattern matches a trailing portion of
          the expanded 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.  If parameter is @ or *, the
          pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant  list.   If
          parameter  is  an  array variable subscripted with @ or *, the pattern removal operation is applied to each member of the
          array in turn, and the expansion is the resultant list.