Regex to match string between brackets
I have a regex that properly returns this:
But I just want to match and return:
I’m using the following command
cmd | grep -i -o -E ‘\[[[:alnum:]]\]’
But I don’t know how to exclude the brackets from the result.
grep -Po '(<=\[)[a-zA-Z0-9]+(?=\])'
tr -d '' also works (Thanks @1_CR for the comment)
... | tr -s "[" "" | tr -s "]" "" is the simplest solution. You can also used
If GNU grep is an option (also this should have been compiled against a recent version of libpcre.so for
\K to work)
grep -oP '\[[[:space:]]*\K[[:alnum:]]+(?=[[:space:]]*\])'
- Database Administration Tutorials
- Programming Tutorials & IT News
- Linux & DevOps World
- Ebook Reviews
- PES Matches, Skills & News