Code Explainers

Code explainers tagged #regex

ruby
class UserAgentParser
  BROWSERS = [
    [/Edg\/([\d.]+)/, "Edge"],
    [/OPR\/([\d.]+)/, "Opera"],

Parsing user-agent strings in Ruby

regex pattern-matching lookup-tables
Intermediate 8 steps
python
import re
from functools import total_ordering
from typing import Optional
 

Parsing and comparing semantic versions

regex operator-overloading sorting
Intermediate 7 steps
java
public final class ImportOrganizer {
 
    private static final Pattern IMPORT_LINE =
            Pattern.compile("^import\\s+(static\\s+)?([\\w.]+(?:\\.\\*)?)\\s*;\\s*$");

Sorting Java imports with a regex pass

regex sorting text-processing
Intermediate 8 steps
typescript
interface ContentRange {
  unit: string;
  start: number;
  end: number;

Parsing HTTP Content-Range headers in TypeScript

regex parsing validation
Intermediate 8 steps
python
import re
 
from flask_wtf import FlaskForm
from wtforms import StringField, TextAreaField, PasswordField

Cleaning form input with Flask-WTF filters

form validation input sanitization filters
Intermediate 8 steps
java
public final class IniParser {
 
    private static final Pattern SECTION = Pattern.compile("\\[(.+?)\\]");
 

Parsing INI files in Java

parsing regex state-machine
Intermediate 10 steps
go
package useragent
 
import (
	"regexp"

Parsing user-agent strings in Go

regex string-parsing ordering
Intermediate 7 steps
go
package payment
 
import (
	"regexp"

Masking card numbers in Go

regex string-processing redaction
Intermediate 6 steps
php
<?php
 
namespace App\Services;
 

Building a profanity filter in PHP

regex text-processing immutability
Intermediate 6 steps
python
import re
from starlette.datastructures import URL
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.responses import RedirectResponse

URL normalization middleware in FastAPI

middleware http-redirects url-canonicalization
Intermediate 8 steps
java
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 

Rendering ${} templates with regex in Java

regex string-templating matcher-api
Intermediate 8 steps
python
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField
from wtforms.validators import (
    DataRequired,

Building a signup form with Flask-WTF

form validation declarative fields validators
Intermediate 7 steps
java
public final class SensitiveDataMasker {
 
    private static final Pattern CARD_NUMBER = Pattern.compile("\\b(?:\\d[ -]*?){13,19}\\b");
    private static final Pattern EMAIL = Pattern.compile("[A-Za-z0-9._%+-]+@([A-Za-z0-9.-]+\\.[A-Za-z]{2,})");

Masking sensitive data with regex in Java

regex data-masking luhn-algorithm
Intermediate 8 steps
java
public final class UserAgentParser {
 
    private static final Pattern OS_PATTERN = Pattern.compile(
            "(Windows NT [0-9.]+|Mac OS X [0-9_]+|Android [0-9.]+|iPhone OS [0-9_]+|Linux)");

Parsing user-agent strings with regex in Java

regex parsing pattern-matching
Intermediate 9 steps
javascript
const IBAN_LENGTHS = {
  DE: 22, FR: 27, GB: 22, ES: 24, IT: 27,
  NL: 18, BE: 16, CH: 21, AT: 20, PT: 25,
};

How IBAN validation works in JavaScript

validation checksum modular-arithmetic
Intermediate 8 steps
typescript
type TokenType = "keyword" | "string" | "comment" | "number" | "text";
 
interface Token {
  type: TokenType;

How a regex tokenizer highlights code

tokenizer regex lexing
Intermediate 10 steps
java
public final class LogRedactor {
 
    private static final Pattern SECRET = Pattern.compile(
        "(?i)(password|token|api[_-]?key|secret|authorization)\\s*[=:]\\s*\\S+");

Streaming log redaction in Java

regex streaming-io try-with-resources
Intermediate 9 steps
go
package handlers
 
import (
	"net/http"

Custom validators and binding in Gin

validation struct-tags error-handling
Intermediate 8 steps