Issue
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.
Solution
You can use it like this:
echo 'some text (some text in paranthesis) [some other in brackets]' |
grep -o '[][()]'
(
)
[
]
Please note that:
- If
]
and[
are placed right after opening[
in a bracket expression then they are treated as literals]
or[
. - That may be an optional
^
at the first place for negation before]
or[
- You don't need to escape
(
and)
inside a bracket expression.
Answered By - anubhava Answer Checked By - Gilberto Lyons (WPSolving Admin)