正则表达式方形紧密支架

时间:2013-04-18 18:14:35

标签: regex bash

我可以使用Bash来匹配一组字符

$ [[ a =~ [abc] ]]; echo $?
0

但是,如果我希望将近方括号]包含在集合中,则会失败

$ [[ a =~ [abc\]] ]]; echo $?
1

$ [[ a =~ [abc\\]] ]]; echo $?
1

1 个答案:

答案 0 :(得分:9)

从手册页regex(7)

To include a literal ']' in the list, make it the first character (fol-
lowing a possible '^').  

测试:

$ [[ "]" =~ []abc] ]]; echo $?
0