The Regex: module provides pattern matching and capture groups using ECMAScript regex syntax (wraps std::regex). Patterns are compiled and cached on first use; a bad pattern returns false / "" / empty list rather than throwing.
| Call | Effect |
|---|---|
Regex:Test(pattern, str, outVar) |
true if pattern matches anywhere in str → outVar |
Regex:Match(pattern, str, outVar) / Regex:IsMatch |
true if pattern matches the entire str → outVar |
Regex:Find(pattern, str, outVar) |
First match text, or "" → outVar |
Regex:Replace(pattern, str, repl, outVar) |
All matches replaced with repl → outVar |
Regex:Groups(pattern, str, listName) |
Populate pre-declared List:String; index 0 = full match, 1+ = capture groups |
Var:Boolean found;
Regex:Test("[0-9]+", "abc 42 def", found);
Konsol:Print(found); // true
Var:String match;
Regex:Find("[a-z]+", "Hello World", match);
Konsol:Print(match); // ello
Var:String result;
Regex:Replace("\\s+", "hello world", " ", result);
Konsol:Print(result); // hello world
List:String groups;
Regex:Groups("(\\w+)@(\\w+)", "user@host", groups);
// groups[0] = "user@host", groups[1] = "user", groups[2] = "host"
Var:String g0; Var:String g1; Var:String g2;
List:Get(groups, 0, g0);
List:Get(groups, 1, g1);
List:Get(groups, 2, g2);
Konsol:Print("${g0} → ${g1} at ${g2}");
// 44_regex.ks — Regex module: pattern matching and capture groups
Konsol:Print("=== Regex ===");
Var:Boolean matched;
Var:String found;
Var:String replaced;
List:String groups;
// Test — true if pattern matches anywhere in the string
Regex:Test("\\d+", "abc 123 def", matched);
Konsol:Print("Test digits in 'abc 123 def' : ${matched}");
Regex:Test("\\d+", "no digits here", matched);
Konsol:Print("Test digits in 'no digits here': ${matched}");
// Match — true only if pattern covers the entire string
Regex:Match("[A-Za-z]+", "hello", matched);
Konsol:Print("Match 'hello' as letters only : ${matched}");
Regex:Match("[A-Za-z]+", "hello123", matched);
Konsol:Print("Match 'hello123' as letters only: ${matched}");
// Find — first match text, or "" if none
Regex:Find("[A-Z][a-z]+", "hello World fooBar", found);
Konsol:Print("Find first capitalised word : ${found}");
Regex:Find("\\d+\\.\\d+", "price is 9.99 dollars", found);
Konsol:Print("Find first decimal : ${found}");
Regex:Find("\\d+", "no numbers", found);
Konsol:Print("Find in string with no match : '${found}'");
// Replace — replaces all matches
Regex:Replace("\\s+", "too many spaces", " ", replaced);
Konsol:Print("Collapse spaces : ${replaced}");
Regex:Replace("[aeiou]", "hello world", "*", replaced);
Konsol:Print("Replace vowels : ${replaced}");
// Groups — index 0 is the full match, 1+ are capture groups
Regex:Groups("(\\w+)@(\\w+)\\.(\\w+)", "user@example.com", groups);
Var:Number groupCount;
List:Size(groups, groupCount);
Konsol:Print("Groups count : ${groupCount}");
Var:String g0;
Var:String g1;
Var:String g2;
Var:String g3;
List:Get(0, groups, g0);
List:Get(1, groups, g1);
List:Get(2, groups, g2);
List:Get(3, groups, g3);
Konsol:Print("Full match : ${g0}");
Konsol:Print("User : ${g1}");
Konsol:Print("Domain : ${g2}");
Konsol:Print("TLD : ${g3}");