Regex Module
The std/regex module provides regular expression utilities for pattern matching, searching, replacing, and splitting strings.
Import
Section titled “Import”import { regex } from "std/regex";Matching
Section titled “Matching”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")); // trueprintln(regex.match("^world", "hello world")); // falseregex.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")); // 4Replacing
Section titled “Replacing”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"Splitting
Section titled “Splitting”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"Capture Groups
Section titled “Capture Groups”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"Validation
Section titled “Validation”regex.isValid(pattern: string): bool
Section titled “regex.isValid(pattern: string): bool”Returns true if the pattern is a valid regular expression.
println(regex.isValid("[0-9]+")); // trueprintln(regex.isValid("[invalid")); // falseFull Example
Section titled “Full Example”import { regex } from "std/regex";
// Extract all email addresses from textconst 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 stringconst 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 whitespaceconst cleaned = regex.replace("[ ]+", " too many spaces ", " ");println(cleaned); // " too many spaces "