Code Explainers
Code explainers tagged #regex
ruby
class TemplateInterpolator PLACEHOLDER = /\{\{\s*([\w.]+)\s*\}\}/ def initialize(strict: false)
Interpolating templates with dotted keys in Ruby
regex
string-interpolation
hash-traversal
Intermediate
6 steps
python
from werkzeug.routing import BaseConverter, ValidationError from flask import Flask, jsonify, abort
Custom URL converters in Flask
url-routing
validation
converters
Intermediate
6 steps
ruby
class Contact < ApplicationRecord PHONE_FORMATS = { "US" => /\A\+1\d{10}\z/, "GB" => /\A\+44\d{10}\z/,
Country-aware phone validation in Rails
validation
callbacks
regex
Intermediate
6 steps
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
javascript
function formatPhoneNumber(value) { const digits = value.replace(/\D/g, '').slice(0, 10); const parts = [];
Building a live phone number input mask
input-masking
regex
dom-events
Intermediate
7 steps
typescript
type MatchSegment = { text: string; matched: boolean; };
Splitting text into highlighted match segments
regex
string-matching
text-highlighting
Intermediate
8 steps
typescript
type NestedValue = string | NestedValue[] | { [key: string]: NestedValue }; function parseFieldPath(name: string): string[] { const match = name.match(/^([^\[\]]+)((?:\[[^\[\]]*\])*)$/);
Parsing bracketed form field names into nested objects
parsing
recursive-types
regex
Intermediate
8 steps
javascript
function escapeHtml(str) { return str.replace(/[&<>"']/g, (ch) => ({ '&': '&', '<': '<',
Safely highlighting search matches in text
html-escaping
regex
search-highlighting
Intermediate
7 steps
php
final class Route { private string $regex; private array $paramNames = [];
Compiling route patterns into regex in PHP
routing
regex
parsing
Intermediate
8 steps
java
import java.util.Comparator; import java.util.LinkedHashMap; import java.util.Map; import java.util.regex.Pattern;
Ranking the most frequent words in Java
streams
regex
grouping
Intermediate
7 steps
java
public final class IbanValidator { private static final Map<String, Integer> COUNTRY_LENGTHS = Map.ofEntries( Map.entry("DE", 22), Map.entry("FR", 27), Map.entry("GB", 22),
Validating IBANs with the mod-97 checksum
validation
checksum
modular-arithmetic
Intermediate
8 steps
python
import re from collections import Counter from pathlib import Path
Ranking the top words in a PDF
text-processing
regex
counting
Intermediate
6 steps
java
public final class Slugifier { private static final Pattern NON_LATIN = Pattern.compile("[^\\w-]"); private static final Pattern WHITESPACE = Pattern.compile("[\\s]+");
Building a URL slugifier in Java
regex
unicode-normalization
string-processing
Intermediate
8 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
typescript
const ISO_DURATION = /^P(?:(\d+(?:\.\d+)?)Y)?(?:(\d+(?:\.\d+)?)M)?(?:(\d+(?:\.\d+)?)W)?(?:(\d+(?:\.\d+)?)D)?(?:T(?:(\d+(?:\.\d+)?)H)?(?:(\d+(?:\.\d+)?)M)?(?:(\d+(?:\.\d+)?)S)?)?$/; const SECONDS_PER = { years: 365 * 24 * 60 * 60,
Parsing ISO 8601 durations to seconds
regex
parsing
iso-8601
Intermediate
7 steps
go
package parser import ( "fmt"
Parsing access logs with named regex groups in Go
regex
named-groups
parsing
Intermediate
5 steps
javascript
function splitFullName(fullName) { const cleaned = (fullName || '').trim().replace(/\s+/g, ' '); if (!cleaned) {
Parsing full names into first and last
string-parsing
regex
edge-cases
Intermediate
8 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