I would like to match these characters: [ ] ( ) in a character class in a regex, how can I do that?
echo 'some text (some text in parenthesis) [some other in brackets]' | grep '[\[\]\(\)]'
This one doesn't match any character.
I would like to match these characters: [ ] ( ) in a character class in a regex, how can I do that?
echo 'some text (some text in parenthesis) [some other in brackets]' | grep '[\[\]\(\)]'
This one doesn't match any character.
The most common way is to put a \ before the character, but oddly enough that doesn't seem to work for ] with grep.
If you can use Perl, do this:
You can use it like this:
You don't need to escape
(
and)
inside a character class. Moreover if you place]
and[
right after opening[
then you don't need to escape them either.Just FYI:
Accoding to the
grep
documentation, section 3.2 Character Classes and Bracket Expressions:Also, you can see that
(
,[
and)
are not special in the bracket expressions.Since
]
in your'[\[\]\(\)]'
bracket expression pattern is not the first character, it ends the pattern, and the next]
created an incomplete bracket expression.