Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex that includes all catch blocks with specific term

Tags:

regex

search

I've recently seen this stackoverflow question that excludes all catch statements with a specific word.

Regex find catch blocks without log

catch\s*\(\s*\w*\s+\w*\s*\)\s*\{(?:[^}](?!\blog\b))*\}

How would you do the opposite?

I've tried switching the negative lookaround part to a positive lookaround, but all that does is grab empty exceptions.

catch\s*\(\s*\w*\s+\w*\s*\)\s*\{(?:[^}](?=\blog\b))*\}

example:

catch (Exception e) 
{
    log.debug("word");
            //stuff
}

I want to find all instances of "log.debug"

like image 556
user Avatar asked Oct 21 '25 04:10

user


1 Answers

You can use a positive lookahead like this:

catch\s*\(\s*\w*\s+\w*\s*\)\s*\{(?=[^\}]*\blog\b)[^\}]+\}

(?=[^\}]*\blog\b) this checks if there's any log in that catch block and match only if there's the word log.

If you want to find log.debug, you simply edit the regex to:

catch\s*\(\s*\w*\s+\w*\s*\)\s*\{(?=[^\}]*\blog\.debug\b)[^\}]+\}
like image 148
Jerry Avatar answered Oct 25 '25 06:10

Jerry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!