27

How do I match word only between parenthesis

Input : this is (test.com)

Desire Output : test.com

Rahul Patil
  • 24,281
  • 25
  • 80
  • 96
  • 1
    Are you looking for a 'grep' regex to match, or, e.g., a 'sed' substitute command to generate your output shown? – rickhg12hs Nov 28 '13 at 23:45

2 Answers2

39

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 (test.com)\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 (test.com)\n" | grep -o '(.*)' | sed 's/[()]//g'
    
  3. Parse the whole thing with Perl:

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

    printf "this is (test.com)\n" | sed 's/.*(\(.*\))/\1/'
    
terdon
  • 234,489
  • 66
  • 447
  • 667
5

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

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

References

slm
  • 363,520
  • 117
  • 767
  • 871