Search code examples
regexlogstashlogstash-grok

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.


Solution

  • 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