Code Explainers

Code explainers tagged #parsing

go
package parser
 
import (
	"fmt"

Parsing access logs with named regex groups in Go

regex named-groups parsing
Intermediate 5 steps
java
public final class AccessLogParser {
 
    private static final Pattern LINE = Pattern.compile(
        "^(?<ip>\\S+) \\S+ \\S+ \\[(?<time>[^\\]]+)\\] "

Parsing Apache access logs in Java

regex named-groups parsing
Intermediate 7 steps
rust
use std::io::{BufRead, BufReader};
use std::process::{Command, Stdio};
 
#[derive(Debug)]

Parsing ps output into structs in Rust

subprocess parsing iterators
Intermediate 8 steps
ruby
class DurationParser
  UNIT_SECONDS = {
    'w' => 604_800,
    'd' => 86_400,

Parsing duration strings into seconds in Ruby

parsing regular-expressions validation
Intermediate 7 steps
javascript
function parseJwtPayload(token) {
  const parts = token.split('.');
  if (parts.length !== 3) {
    throw new Error('Invalid JWT: expected 3 segments');

Decoding a JWT payload in JavaScript

jwt base64url encoding
Intermediate 6 steps
ruby
class SemanticVersion
  include Comparable
 
  attr_reader :major, :minor, :patch, :prerelease

Comparable semantic versions in Ruby

comparable parsing operator-overloading
Intermediate 8 steps
php
<?php
 
namespace App\Config;
 

Writing a .env file loader in PHP

parsing environment-variables configuration
Intermediate 9 steps
typescript
type JsonValue = Record<string, unknown> | unknown[];
 
export function parseJsonStream<T = JsonValue>(
  stream: ReadableStream<Uint8Array>,

Streaming JSON parsing with a depth counter

streams parsing state-machine
Advanced 9 steps
rust
use std::num::ParseIntError;
 
fn parse_line(line: &str) -> Result<Vec<i64>, ParseIntError> {
    line.split(',')

Collecting Results in Rust iterators

iterators error-handling result
Intermediate 7 steps
ruby
require "time"
require "tzinfo"
 
module TimeConverter

Converting timestamps across time zones with TZInfo

timezones iso8601 parsing
Intermediate 8 steps
rust
use std::collections::HashMap;
use std::fmt;
 
#[derive(Debug)]

Parsing a config file with typed errors in Rust

error-handling enums parsing
Intermediate 9 steps
java
public final class QueryParams {
 
    private final Map<String, List<String>> params;
 

Parsing and building URL query strings in Java

parsing multimap url-encoding
Intermediate 9 steps
python
import re
from collections import defaultdict
from pathlib import Path
 

Summarizing log files by date in Python

regex parsing aggregation
Intermediate 7 steps
python
import re
 
_UNIT_SECONDS = {
    "w": 604800,

Parsing duration strings like 1h30m in Python

regex parsing validation
Intermediate 8 steps
python
import re
from datetime import datetime
from dataclasses import dataclass
 

Parsing access logs with named regex groups

regex named-groups dataclasses
Intermediate 6 steps