ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Perl Regex for Efficient Log Line Filtering

Perl

Goal -- WPM

Ready
Exercise Algorithm Area
1sub is_critical_error_log {
2my ($log_line) = @_;
3
4# Optimized regex to detect critical errors in log lines.
5# It looks for 'ERROR' or 'CRITICAL' keywords, optionally preceded by timestamp/level info.
6# Uses atomic grouping and non-capturing groups for efficiency.
7my $critical_regex = qr/^
8(?:[\d\s-:]+)?
9(?:\s+)?
10(?:INFO|DEBUG|WARN|TRACE)?
11(?:\s+)?
12(?:\(?(?:ERROR|CRITICAL)\)?)
13(?:\s+)?
14(?!.*(?:INFO|DEBUG|TRACE|WARN))
15.*$/xi;
16
17return $log_line =~ $critical_regex;
18}
19
20# Example Usage:
21# print is_critical_error_log("2023-10-10 10:00:00 ERROR: Something went wrong\n") ? "Match\n" : "No Match\n";
22# print is_critical_error_log("10/Oct/2023:13:55:36 [INFO] Request processed\n") ? "Match\n" : "No Match\n";
23# print is_critical_error_log("2023-10-10 10:05:00 CRITICAL: System failure detected\n") ? "Match\n" : "No Match\n";
24# print is_critical_error_log("WARN: Low disk space\n") ? "Match\n" : "No Match\n";
Algorithm description viewbox

Perl Regex for Efficient Log Line Filtering

Algorithm description:

This Perl script defines a function `is_critical_error_log` that uses a highly optimized regular expression to identify critical error messages within log lines. The regex is designed for speed by minimizing backtracking and using non-capturing groups. It specifically targets lines containing 'ERROR' or 'CRITICAL' keywords, while ensuring they are not misclassified as less severe log levels like 'INFO' or 'DEBUG' using a negative lookahead. This is essential for real-time log monitoring and alerting systems.

Algorithm explanation:

The `is_critical_error_log` function employs a single, optimized regular expression for efficient log line filtering. The regex `qr/.../xi` is compiled for speed (`x` for free-spacing/comments, `i` for case-insensitivity). It uses non-capturing groups `(?:...)` to avoid overhead. The core logic is `(?:\(?ERROR|CRITICAL)\)?`, which matches 'ERROR' or 'CRITICAL', optionally surrounded by parentheses. A negative lookahead `(?!.*(?:INFO|DEBUG|TRACE|WARN))` is crucial: it ensures that if any of the less severe keywords appear *after* the potential error keyword on the same line, the match fails. This prevents false positives. The initial parts of the regex `(?:[\d\s-:]+)?(?:\s+)?(?:INFO|DEBUG|WARN|TRACE)?(?:\s+)?` are designed to match common log prefixes (timestamps, levels) without consuming too much time or causing excessive backtracking. The time complexity is effectively O(L) where L is the length of the log line, as the regex engine attempts a linear scan. Space complexity is O(1) as it only stores the regex and a boolean result.

Pseudocode:

Function is_critical_error_log(log_line):
  Define optimized_critical_error_regex.
  Return true if log_line matches optimized_critical_error_regex, false otherwise.