Appearance
Find and replace with regular expressions
std/regex is regular expressions in a linear-time dialect: no pattern can make a match take more than time proportional to the text, so a pattern from a user or a config file can't hang the server. A regex"…" literal is checked when the file compiles, and reads as you'd write it in JS (\d is a digit, no doubled backslashes).
Parse log lines
lib/logs.weft
weft
import { regex, find, findAll } from "std/regex"
export type Entry = { time: string, level: string, message: string }
let line = regex"^(?<time>\d{2}:\d{2}:\d{2}) \[(?<level>[A-Z]+)\] (?<message>.*)$"
/// One entry per line that looks like `12:03:44 [WARN] disk almost full`; other lines are skipped.
export fn parse(log: string): Entry[] {
let mut out: Entry[] = []
for text of log.split("\n") {
let m = line.find(text)
if m == null { continue }
out.push({ time: m.named.get("time") ?? "", level: m.named.get("level") ?? "", message: m.named.get("message") ?? "" })
}
return out
}
/// Every `#tag` in a message.
export fn tags(message: string): string[] {
return regex"#(\w+)".findAll(message).map(m => m.groups[1] ?? "")
}find gives the first match with its groups (m.groups[1], or by name with m.named), findAll every match. The functions take the Regex first, so line.find(text) is find(line, text).
Clean up text
lib/slug.weft
weft
import { regex, replaceAll, replaceWith, split, Match } from "std/regex"
/// `"Hello, World! Again"` → `"hello-world-again"`.
export fn slug(title: string): string {
let words = regex"[^\p{L}\p{N}]+".split(title.toLowerCase())
return words.filter(w => w != "").join("-")
}
/// Dates written `17/11/2026` rewritten as `2026-11-17`.
export fn isoDates(text: string): string {
return regex"\b(\d{2})/(\d{2})/(\d{4})\b".replaceAll(text, "$3-$2-$1")
}
/// Each word capitalised.
export fn titleCase(text: string): string {
return regex"\b\p{Ll}".replaceWith(text, (m: Match) => m.text.toUpperCase())
}In a replacement, $1 is a group, $<name> a named one and $& the whole match; replaceWith calls a function for each match instead.
Patterns from outside
A pattern that isn't known when the code is written (a search box, a config file) is made with compile, which fails with a PatternError for text that isn't a pattern. Put user text into a literal pattern as a value instead: {name} matches its text exactly, whatever characters it has.
lib/search.weft
weft
import { regex, compile, test } from "std/regex"
import type { PatternError } from "std/regex"
/// Whether `text` has `word` as a whole word, ignoring case (`word` is matched literally).
export fn hasWord(text: string, word: string): boolean {
return regex"(?i)\b{word}\b".test(text)
}
/// A filter from the settings: a pattern the admin typed (failing when it isn't one).
export fn matchesFilter(pattern: string, text: string): boolean ! PatternError {
return (try compile(pattern, "i")).test(text)
}The dialect is RE2's: groups, classes, Unicode properties (\p{L}), lazy repetition (*?), anchors and \b, but no backreferences or lookaround, which are what make a backtracking engine slow. The reference is std/regex.