Skip to content

Regex Module

The std/regex module provides regular expression utilities for pattern matching, searching, replacing, and splitting strings.

import { regex } from "std/regex";

regex.match(pattern: string, str: string): bool

Section titled “regex.match(pattern: string, str: string): bool”

Returns true if the pattern matches anywhere in the string.

println(regex.match("^hello", "hello world")); // true
println(regex.match("^world", "hello world")); // false

regex.find(pattern: string, str: string): string

Section titled “regex.find(pattern: string, str: string): string”

Returns the first match of the pattern, or an empty string if no match.

println(regex.find("[0-9]+", "abc 123 def 456")); // "123"

regex.findAll(pattern: string, str: string): any

Section titled “regex.findAll(pattern: string, str: string): any”

Returns all matches as an array.

const matches = regex.findAll("[0-9]+", "abc 123 def 456");
println(matches[0]); // "123"
println(matches[1]); // "456"

regex.count(pattern: string, str: string): int

Section titled “regex.count(pattern: string, str: string): int”

Counts the number of non-overlapping matches.

println(regex.count("[0-9]+", "a1b2c3d4")); // 4

regex.replace(pattern: string, str: string, replacement: string): string

Section titled “regex.replace(pattern: string, str: string, replacement: string): string”

Replaces all matches of the pattern with the replacement string.

const result = regex.replace("[0-9]+", "abc 123 def 456", "NUM");
println(result); // "abc NUM def NUM"

regex.split(pattern: string, str: string): any

Section titled “regex.split(pattern: string, str: string): any”

Splits a string by the pattern, returning an array of parts.

const parts = regex.split(",[ ]*", "a, b, c, d");
println(parts[0]); // "a"
println(parts[1]); // "b"
println(parts[2]); // "c"
println(parts[3]); // "d"

regex.groups(pattern: string, str: string): any

Section titled “regex.groups(pattern: string, str: string): any”

Returns an array of capture group values from the first match (excludes the full match).

const groups = regex.groups("([a-z]+)@([a-z]+)[.]([a-z]+)", "user@example.com");
println(groups[0]); // "user"
println(groups[1]); // "example"
println(groups[2]); // "com"

Returns true if the pattern is a valid regular expression.

println(regex.isValid("[0-9]+")); // true
println(regex.isValid("[invalid")); // false
import { regex } from "std/regex";
// Extract all email addresses from text
const text = "Contact us at info@example.com or support@chuks.org";
const emails = regex.findAll("[a-zA-Z0-9.]+@[a-zA-Z0-9.]+", text);
for (var i = 0; i < len(emails); i++) {
println("Found email: " + emails[i]);
}
// Parse a structured string
const logLine = "2025-06-15 14:30:00 [ERROR] Connection failed";
const parts = regex.groups("([0-9-]+) ([0-9:]+) \\[([A-Z]+)\\] (.+)", logLine);
println("Date: " + parts[0]);
println("Time: " + parts[1]);
println("Level: " + parts[2]);
println("Message: " + parts[3]);
// Clean up whitespace
const cleaned = regex.replace("[ ]+", " too many spaces ", " ");
println(cleaned); // " too many spaces "