Skip to content

Regex

Regular expression matching using POSIX extended regex syntax.

Functions

Regex.match(text: string, pattern: string) -> bool

Returns true if the text matches the pattern.

wyn
println(Regex.match("hello123", "[a-z]+[0-9]+").to_string())  // true
println(Regex.match("hello", "^[0-9]+$").to_string())          // false
println(Regex.match("42", "^[0-9]+$").to_string())             // true

Regex.replace(text: string, pattern: string, replacement: string) -> string

Replaces all matches of the pattern with the replacement string.

wyn
var result = Regex.replace("foo 123 bar 456", "[0-9]+", "NUM")
println(result)  // foo NUM bar NUM

Regex.find_all(text: string, pattern: string) -> string

Returns all matches as a newline-separated string.

wyn
var matches = Regex.find_all("abc 123 def 456", "[0-9]+")
println(matches)
// 123
// 456

Pattern Syntax

Uses POSIX Extended Regular Expressions (ERE):

PatternMatches
.Any character
[a-z]Character class
[^0-9]Negated class
^ / $Start / end of string
*Zero or more
+One or more
?Zero or one
{n,m}Between n and m
(...)Group
|Alternation

Example: Email Validation

wyn
fn is_email(s: string) -> bool {
    return Regex.match(s, "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$")
}

println(is_email("user@example.com").to_string())  // true
println(is_email("not-an-email").to_string())       // false

Platform Note

Regex works on all platforms. On POSIX systems (macOS, Linux), the native <regex.h> is used. On Windows, Wyn includes a built-in NFA-based regex engine (Thompson's construction, guaranteed O(n·m) — no exponential backtracking).

MIT License