이 zsh 매개변수 확장은 어떻게 작동합니까?

이 zsh 매개변수 확장은 어떻게 작동합니까?

이 매개변수 확장이 포함된 zsh 스크립트를 발견했습니다.

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

이 표현의 맥락을 알고 싶은 사람은 누구나 이 표현의 일부를 볼 수 있습니다.약어와 같은 vim을 제공하는 zle 위젯 기능

기본 형태는 분명히 접미사 트림입니다. 즉${name%%pattern}

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]#}

BRE 또는 PCRE 와 같은 (#m)변수에서 일치하는 패턴을 사용할 수 있는 위치 에서 시작하여 정규식 스타일 역참조를 시작합니다 . 그리고 와 같기 때문에 문자 집합에서 0개 이상의 문자와 일치합니다 .$MATCH\1$1
#*[_a-zA-Z0-9]#[_a-zA-Z0-9]

관련 정보