Search code examples
regexrubyfluent-bit

Ruby regex for fluentbit


I need to create a regex expression for fluent bit This is what I tried

^(?<log_time>[0-9-]+ [:0-9,]+)\s+\s+(?<severity>\w+)\s+-\s+(?<message>.*)

Input is 2022-07-20 15:21:31,994 - INFO - Moving to Dashboard

Desired output:

    log_time:  2022-07-20 15:21:31,994
    severity:  INFO
    message:   Moving to Dashboard

How can I achieve this? (at some point I am getting the log_time before milliseconds but that's not enough) Help would be appreciated. Thank you


Solution

  • You can use

    ^(?<log_time>[0-9-]+ [:0-9,]+)\s+-\s+(?<severity>\w+)\s+-\s+(?<message>.*)
    

    See the regex demo. Details:

    • ^ - start of string
    • (?<log_time>[0-9-]+ [:0-9,]+) - Log time: one or more digits or -, then a space, and then one or more colons, digit or commas
    • \s+-\s+ - a hyphen wrapped with one or more whitespaces
    • (?<severity>\w+) - Severity: one or more word chars
    • \s+-\s+ - a hyphen wrapped with one or more whitespaces
    • (?<message>.*) - Message: any zero or more chars other than line break chars as many as possible (no need of $)