Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex java exception exclude "Caused by:"

I am trying to capture the Java exception anem from a log file, but I would like to exclude the "Caused by:" string using Oniguruma Regular Expressions Version 6.0.0:

^.+Exception

returns:

"Caused by: java.nio.file.NoSuchFileException"

How can I write a regular expression which captures the exception name but ignores the "Caused by: " string in front of it? This will be used in Grok Logstash.

like image 346
Arturski Avatar asked Jan 17 '26 04:01

Arturski


1 Answers

You may use capturing with the following pattern:

^Caused\s*by:\s*(?<exception>[\w.]+)

The (?<exception>[\w.]+) named capturing group will match and capture into Group "exception" (creating the variable with the same name) 1+ word (letters, digits or underscores) or . chars.

Answering your question, to check and require something but excluding it from the match, you may use lookarounds:

(?<=^Caused by: ).*?Exception

See this Rubular demo. And below is a test at https://grokdebug.herokuapp.com/:

enter image description here

like image 119
Wiktor Stribiżew Avatar answered Jan 19 '26 20:01

Wiktor Stribiżew



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!