grep: regex only for match anything between parenthesis

Here are a few options, all of which print the desired output:

  1. Using grep with the -o flag (only print matching part of line) and Perl compatible regular expressions (-P) that can do lookarounds:

    printf "this is (\n" | grep -Po '(?<=\().*(?=\))'

    That regex might need some explaining:

    • (?<=\() : This is a positive lookbehind, the general format is (?<=foo)bar and that will match all cases of bar found right after foo. In this case, we are looking for an opening parenthesis, so we use \( to escape it.

    • (?=\)) : This is a positive lookahead and simply matches the closing parenthesis.

  2. The -o option to grep causes it to only print the matched part of any line, so we look for whatever is in parentheses and then delete them with sed:

    printf "this is (\n" | grep -o '(.*)' | sed 's/[()]//g'
  3. Parse the whole thing with Perl:

    printf "this is (\n" | perl -pe 's/.*\((.+?)\)/$1/'
  4. Parse the whole thing with sed:

    printf "this is (\n" | sed 's/.*(\(.*\))/\1/'

One approach would be to use PCRE - Perl Compatible Regular Expressions with grep:

$ echo "this is (" | grep -oP '(?<=\().*(?=\))'


  • - lookarounds