ankit
ankit

Reputation: 1529

Error in Grep regex

I have written following regex in grep command to find all the catch statements for SQLException in a directory containing java files.

catch\s{0,}\(s{0,}SQLException

it is giving "grep: Unmatched ( or (" error. This regex is working fine on RegExr.com. What is the issue with this regex?

Upvotes: 2

Views: 181

Answers (2)

anubhava
anubhava

Reputation: 785896

grep uses BRE by default and \( becomes start of a captured group (unescaped ( is taken literally).

You can use this grep with extended regex mode -E:

grep -E 'catch[[:blank:]]*\([[:blank:]]*SQLException' file.log

Upvotes: 1

falsetru
falsetru

Reputation: 369414

To use \s, you need to specify -P option (to allow Perl regular expression):

grep -P 'catch\s{0,}\(\s{0,}SQLException' 

BTW, you can use * instead of {0,}.

Upvotes: 1

Related Questions