4

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

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

]但是,如果我想在集合中包含一个右方括号,它会失败

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

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

1 回答 1

9

从手册页regex(7)

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

测试:

$ [[ "]" =~ []abc] ]]; echo $?
0
于 2013-04-18T18:23:13.637 回答