Code Explainers
Code explainers tagged #regex
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
python
import re import unicodedata _SLUG_STRIP = re.compile(r"[^\w\s-]")
Building URL-safe slugs in Python
string-processing
regex
unicode-normalization
Intermediate
7 steps
ruby
class SignupValidator EMAIL_REGEX = /\A[^@\s]+@[^@\s]+\.[^@\s]+\z/ MIN_PASSWORD_LENGTH = 8
Building a plain-Ruby signup validator
validation
regex
default-hash
Intermediate
7 steps
rust
use std::fs; use std::path::Path; use regex::Regex;
Redacting emails in a file with Rust
regex
file-io
closures
Intermediate
6 steps
python
import logging import re
Redacting secrets in Python log records
logging
regex
redaction
Intermediate
7 steps
javascript
const RESERVED_SLUGS = new Set(['new', 'edit', 'admin', 'api']); function slugify(title) { return title
Generating URL-safe unique slugs
string-normalization
regex
deduplication
Intermediate
9 steps