$BASH_REMATCH[] 的 zsh 等价物是什么? [英] What is the zsh equivalent for $BASH_REMATCH[]?

查看:20
本文介绍了$BASH_REMATCH[] 的 zsh 等价物是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

zsh$BASH_REMATCH 的等价物是什么,它是如何使用的?

What is the equivalent in zsh for $BASH_REMATCH, and how is it used?

推荐答案

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

To make zsh behave the same as bash, use:

setopt BASH_REMATCH

或者在一个函数中考虑:

Or within a function consider:

setopt local_options BASH_REMATCH

(这只会设置函数范围内的选项)

(this will only set the option within the scope of the function)

然后像在 bash 中一样使用 $BASH_REMATCH.

Then just use $BASH_REMATCH as you would in bash.

手册 介绍了 BASH_REMATCH:

设置后,使用 =~ 运算符执行的匹配将设置 BASH_REMATCH 数组变量,而不是默认的 MATCH 和匹配变量.BASH_REMATCH 数组的第一个元素将包含整个匹配的文本,后续元素将包含提取的子字符串.当 KSH_ARRAYS 也被设置时,这个选项更有意义,这样整个匹配的部分存储在索引 0 处,第一个子字符串在索引 1 处. 如果没有这个选项,MATCH 变量包含整个匹配的文本,匹配数组变量包含子字符串.

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 中的一样,但如果您想要 手册:

Then =~ will behave like in bash, but if you want the full behaviour as described in the manual:

字符串 =~ 正则表达式

string =~ regexp

如果字符串与正则表达式regexp匹配,则为真.如果选项 RE_MATCH_PCRE 设置为 regexp,则使用 zsh/pcre 模块将其作为 PCRE 正则表达式进行测试,否则使用 zsh/regex 模块将其作为 POSIX 扩展正则表达式进行测试.匹配成功后,会更新一些变量;如果匹配失败,则不会更改任何变量.

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.

如果选项 BASH_REMATCH 未设置,则标量参数 MATCH 设置为匹配模式的子字符串,整数参数 MBEGIN 和 MEND 分别设置为字符串中匹配项的开始和结束的索引,这样如果字符串包含在变量 var 中,表达式 '${var[$MBEGIN,$MEND]}' 与 '$MATCH' 相同.尊重选项 KSH_ARRAYS 的设置.同样,数组 match 设置为匹配括号子表达式的子字符串,数组 mbegin 和 mend 分别设置为字符串中子字符串的开始和结束位置的索引.如果没有带括号的子表达式,则不会设置数组.例如,如果字符串 'a short string' 与正则表达式 's(...)t' 匹配,则(假设选项 KSH_ARRAYS 未设置)MATCH、MBEGIN 和 MEND 为 'short'、3 和 7,而 match、mbegin 和 mend 分别是包含字符串 'hor'、'4' 和 '6' 的单项数组.

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.

如果设置了 BASH_REMATCH 选项,则数组 BASH_REMATCH 将设置为与模式匹配的子字符串,后跟与模式内带括号的子表达式匹配的子字符串.

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.

这篇关于$BASH_REMATCH[] 的 zsh 等价物是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆