- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
zsh
中的$BASH_REMATCH
等价于什么,如何使用?
最佳答案
要使zsh
的行为与bash
相同,请使用:
setopt BASH_REMATCH
或在函数内考虑:
setopt local_options BASH_REMATCH
(这只会在功能范围内设置选项)
$BASH_REMATCH
中那样使用
bash
即可。
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/
zsh中的$BASH_REMATCH等价于什么,如何使用? 最佳答案 要使zsh的行为与bash相同,请使用: setopt BASH_REMATCH 或在函数内考虑: setopt local_op
我试图在 bash 中捕获路径的一部分: 输入:/Users/foo/.virtualenvs/venv-test-server 代码: #!/bin/zsh regex="^.*\/venv-(.*
上下文 我正在尝试使用正则表达式从 github https 克隆链接中提取存储库名称,但我似乎无法让 BASH_REMATCH 工作。 对于上下文,我将其编写为 .sh 文件并在 Git Bash
我在使用正则表达式时遇到问题,我需要搜索并删除与正则表达式匹配的模式,当找到时我需要修剪掉。我写了一个这样的正则表达式 regex='(.*)((aa[[:space:]]bb)|(awd)|(bab
我想使用 bash 运算符 +~ 首先拆分一个字符串,然后再次检查生成的字符串。当我尝试这个时,如果发现 BASH_REMATCH 的元素被第二次使用覆盖,所以 mother-function 在第二
代码是这样的: #!/bin/bash if [[ foobarbletch =~ 'foo(bar)bl(.*)' ]] then echo "The regex matches!"
我正在尝试使用以下正则表达式在 FOO_FOO_FOO_BAR_BAR 中捕获 BAR_BAR:(?:.*?_){3}(.*). 正则表达式在使用 RegExr 或 regex101 等验证器时有效,
我在 release notes of Bash 5.1 中读到: p. BASH_REMATCH is no longer readonly. 如 Bash Reference Manual 中所述
假设我有一个(愚蠢的例子)脚本: #!/bin/bash st="one two three" [[ $st =~ ^([[:alpha:]]+)[[:space:]]([[:alpha:]]+)[[
我是一名优秀的程序员,十分优秀!