regex - bash 中的@(...|...|...) 语法是什么?

标签 regex bash shell scripting

关注此 question 中提到的 @() 语法:

[[ $OSTYPE == *@(darwin|freebsd|solaris|cygwin|openbsd)* ]]

这个语法从何而来?为什么在这种情况下使用它?和 有什么区别:

[[ $OSTYPE =~ (darwin|freebsd|solaris|cygwin|openbsd) ]]

[[ $OSTYPE == *(darwin|freebsd|solaris|cygwin|openbsd) ]]

这一切似乎都是等价的。

是否使用此语法代替 =~ 运算符以提高正则表达式的可移植性?

谢谢你的解释

最佳答案

man bash 中搜索 extglob 选项:

If the extglob shell option is enabled using the shopt builtin, several extended pattern matching operators are recognized. In the following description, a pattern-list is a list of one or more patterns separated by a |. Composite patterns may be formed using one or more of the following sub-patterns:

?(pattern-list)
    Matches zero or one occurrence of the given patterns
*(pattern-list)
    Matches zero or more occurrences of the given patterns
+(pattern-list)
    Matches one or more occurrences of the given patterns
@(pattern-list)
    Matches one of the given patterns
!(pattern-list)
    Matches anything except one of the given patterns

关于regex - bash 中的@(...|...|...) 语法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35812293/

相关文章:

c++ - 如何将程序变成守护程序

linux - BASH 后台进程打印消失提示行

ruby-on-rails - 如何在 vim/sublime 中很好地显示 Rails development.log?

shell - shell : Integer expression expected error [duplicate]

linux - bash + Linux + 如何忽略字符 "!"

c - 如何在 Linux 中检查信号处理程序

regex - 在 Perl 中使用正则表达式解析属性

ruby - 使用 Regexp 部分匹配 Ruby 字符串的方法

javascript - 在 Ruby/Javascript 中解码带引号的可打印字符串

java - 正则表达式不喜欢 out#