正则表达式-字符类中的括号和括号

nag*_*hun 2 regex grep special-characters

我想匹配以下字符:[]()在正则表达式的字符类中,该怎么做?

echo 'some text (some text in parenthesis) [some other in brackets]' | grep '[\[\]\(\)]'
Run Code Online (Sandbox Code Playgroud)

这个人与任何字符都不匹配。

anu*_*ava 5

您可以像这样使用它:

echo 'some text (some text in paranthesis) [some other in brackets]' | grep -o '[][()]'

(
)
[
]
Run Code Online (Sandbox Code Playgroud)

你不需要逃避()字符类中。而且,如果您在打开后放置]并就位,那么您也不需要逃脱它们。[[