gpt4 book ai didi

zsh - $ BASH_REMATCH []的zsh等效项是什么?

转载 作者:行者123 更新时间:2023-12-03 15:21:48 25 4
gpt4 key购买 nike

zsh中的$BASH_REMATCH等价于什么,如何使用?

最佳答案

要使zsh的行为与bash相同,请使用:

setopt BASH_REMATCH
或在函数内考虑:
setopt local_options BASH_REMATCH
(这只会在功能范围内设置选项)
然后,只需像在 $BASH_REMATCH中那样使用 bash即可。

manual说出有关 BASH_REMATCH的信息:

When set, matches performed with the =~ operator will set the BASH_REMATCH array variable, instead of the default MATCH and match variables. The first element of the BASH_REMATCH array will contain the entire matched text and subsequent elements will contain extracted substrings. This option makes more sense when KSH_ARRAYS is also set, so that the entire matched portion is stored at index 0 and the first substring is at index 1. Without this option, the MATCH variable contains the entire matched text and the match array variable contains substrings.


然后 =~的行为将类似于 bash,但是如果您想要 manual中描述的完整行为,请执行以下操作:

string =~ regexp

true if string matches the regular expression regexp. If the option RE_MATCH_PCRE is set regexp is tested as a PCRE regular expression using the zsh/pcre module, else it is tested as a POSIX extended regular expression using the zsh/regex module. Upon successful match, some variables will be updated; no variables are changed if the matching fails.

If the option BASH_REMATCH is not set the scalar parameter MATCH is set to the substring that matched the pattern and the integer parameters MBEGIN and MEND to the index of the start and end, respectively, of the match in string, such that if string is contained in variable var the expression ‘${var[$MBEGIN,$MEND]}’ is identical to ‘$MATCH’. The setting of the option KSH_ARRAYS is respected. Likewise, the array match is set to the substrings that matched parenthesised subexpressions and the arrays mbegin and mend to the indices of the start and end positions, respectively, of the substrings within string. The arrays are not set if there were no parenthesised subexpresssions. For example, if the string ‘a short string’ is matched against the regular expression ‘s(...)t’, then (assuming the option KSH_ARRAYS is not set) MATCH, MBEGIN and MEND are ‘short’, 3 and 7, respectively, while match, mbegin and mend are single entry arrays containing the strings ‘hor’, ‘4’ and ‘6’, respectively.

If the option BASH_REMATCH is set the array BASH_REMATCH is set to the substring that matched the pattern followed by the substrings that matched parenthesised subexpressions within the pattern.

关于zsh - $ BASH_REMATCH []的zsh等效项是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53030233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com