Code Explainers

Code explainers tagged #named-captures

rust
use once_cell::sync::Lazy;
use regex::Regex;
 
static LOG_LINE: Lazy<Regex> = Lazy::new(|| {

Parsing access logs with a lazy regex in Rust

regex lazy-initialization parsing
Intermediate 7 steps
ruby
class ApacheLogParser
  LINE_PATTERN = /\A(?<ip>\S+)\s\S+\s\S+\s\[(?<time>[^\]]+)\]\s"(?<method>[A-Z]+)\s(?<path>\S+)\s(?<protocol>[^"]+)"\s(?<status>\d{3})\s(?<bytes>\d+|-)/
 
  TIME_FORMAT = "%d/%b/%Y:%H:%M:%S %z"

Parsing Apache logs with named captures

regex named-captures parsing
Intermediate 6 steps