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"
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)[^\}]+\}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With