このzshパラメータ拡張はどのように機能しますか?

このzshパラメータ拡張はどのように機能しますか?

このパラメータ拡張を含むzshスクリプトを見つけました。

${LBUFFER%%(#m)[_a-zA-Z0-9]#}

この表現が発見された文脈を気になる人なら誰でも知ることができます。vimに似た略語を提供するzleウィジェット機能

基本的な形は明らかにサフィックスの剪定です。つまり${name%%pattern}

つまり、from man zshexpn- パラメータ拡張

   ${name%pattern}
   ${name%%pattern} 
   If the pattern matches the end of the value of name,  then  sub‐
      stitute the value of name with the matched portion deleted;
     [...]

~についてパラメータ拡張フラグ (#m):からman zshexpn

   #      Evaluate the resulting words as numeric expressions  and    output
      the  characters  corresponding  to  the resulting integer.  Note
      that this form is entirely distinct from use of  the  #  without
      parentheses.


   m      Only  useful together with one of the flags l or r or with the #
      length operator when the MULTIBYTE option is in effect.  Use the
      character  width  reported by the system in calculating how much
      of the string it occupies or the overall length of  the  string.
      Most printable characters have a width of one unit, however cer‐
      tain Asian character sets and certain special effects use  wider
      characters; combining characters have zero width.  Non-printable
      characters are arbitrarily counted as zero width; how they would
      actually be displayed will vary.

この[_a-zA-Z0-9]#部分は明らかに文字列の末尾から削除するパターンですが、LBUFFER正規表現パターンですか、それとも一種の正規表現グローブミックスですか?
zsh関連の「extended_glob」モードの一部ですか?つまり、man zshoptions

   EXTENDED_GLOB
      Treat  the  `#',  `~' and `^' characters as part of patterns for
      filename generation, etc.  (An initial unquoted `~' always  pro‐
      duces named directory expansion.)

このzshパラメータ拡張は何をしますか?

ベストアンサー1

これはzsh「拡張glob」表現のようです。

つまり、man zshexpn

ワイルドカードフラグ
を含むグループの終わりまでまたはパターンの終わりまで、右側のすべてのテキストに影響を与えるいくつかのフラグがあり、EXTENDED_GLOBオプションが必要です。すべて(#X)形式を取ります。ここで、Xは次の形式のいずれかを取ることができます。

[...]
m は、
一致文字列全体に対する一致データへの参照を設定します。これは逆参照と似ており、ファイル名の作成には影響しません。フラグはパターンの終わりに有効でなければなりません。つまり、グループにローカルではないはずです。 $ MATCH、$ MBEGIN、および$ MENDパラメーターは、それぞれ一致する文字列と文字列の先頭と末尾のインデックスに設定されます。これは、一致する文字列が異なる場合に明らかなパラメータ置換に最も便利です。

例えば、

       arr=(veldt jynx grimps waqf zho buck)
       print ${arr//(#m)[aeiou]/${(U)MATCH}}

すべての一致(つまり、すべてのコレクション)を大文字に強制して、「vEldt jynx grImps wAqf zhO bUck」を印刷します。逆参照とは異なり、例が示されている場合は、文字列の置換に必要な追加の置換を除いて、一致参照を使用すると速度の低下はありません。

この#演算子はいわゆる「クロージャ」または反復一致演算子であり、正規表現*と同じです。

ここで説明されているようにhttp://zsh.sourceforge.net/Guide/zshguide05.html#l139

したがって、デフォルトでは、このパラメータ拡張は次のようになります。

${LBUFFER%%(#m)[_a-zA-Z0-9]#}

(#m)正規表現スタイルの逆参照は、変数(BREやPCREなど)で一致するパターンを使用できる場所から始まります。 likeは文字セット内のゼロ個以上の文字と一致するためです。$MATCH\1$1
#*[_a-zA-Z0-9]#[_a-zA-Z0-9]

おすすめ記事