regex
stableCompiled regular expression matching using the Rust regex crate, with support for test, find, find-all, capture groups, replacement, and splitting.
use plugin regex::{Regex, is_match, find, …} Functions (8)
- Regex Compiles a regular expression pattern.
- is_match Test if pattern matches anywhere in text
- find Find first match position and text
- find_all Find all non-overlapping matches
- captures Extract capture groups from first match
- replace Replace first match with a string
- replace_all Replace all matches with a string
- split Split text on pattern boundaries
Compiles a regular expression pattern.
Compiles a regular expression pattern. Errors if the pattern is invalid. The compiled regex can be reused across multiple calls without recompiling.
use plugin regex::{Regex}
let re = Regex("\\d+")
let email_re = Regex("[a-zA-Z0-9._%+\\-]+@[a-zA-Z0-9.\\-]+\\.[a-zA-Z]{2,}")
Test if pattern matches anywhere in text
Returns true if the pattern matches anywhere in text.
use plugin regex::{Regex}
let re = Regex("^hello")
print(re.is_match("hello world")) // true
print(re.is_match("say hello")) // false
Find first match position and text
Finds the first match and returns #{text, start, end}, where start and end are byte offsets. Returns nil if there is no match.
use plugin regex::{Regex}
let re = Regex("\\d+")
let m = re.find("order #42 shipped")
if m != nil {
print("found '{m["text"]}' at offset {m["start"]}")
}
Find all non-overlapping matches
Returns a table of all non-overlapping matches. Each entry has text, start, and end.
use plugin regex::{Regex}
let re = Regex("\\b\\w{4}\\b")
let matches = re.find_all("find all four char words here")
for _, m in matches {
print(m["text"])
}
Extract capture groups from first match
Returns the capture groups for the first match as a table indexed from 0. Index 0 is the full match; higher indices are capture groups in order. Returns nil if there is no match.
use plugin regex::{Regex}
let re = Regex("(\\d{4})-(\\d{2})-(\\d{2})")
let caps = re.captures("event on 2026-06-09")
if caps != nil {
print("year: {caps[1]}, month: {caps[2]}, day: {caps[3]}")
}
Replace first match with a string
Replaces the first match with replacement. Use $1, $2, etc. in the replacement to refer to capture groups.
use plugin regex::{Regex}
let re = Regex("(\\w+) (\\w+)")
let result = re.replace("John Doe", "$2, $1")
print(result) // Doe, John
Replace all matches with a string
Replaces every non-overlapping match with replacement. Capture group references work the same as replace.
use plugin regex::{Regex}
let re = Regex("\\d+")
let masked = re.replace_all("call 555-1234 or 555-5678", "***")
print(masked) // call ***-*** or ***-***
Split text on pattern boundaries
Splits text at every match of the pattern and returns a table of the resulting substrings.
use plugin regex::{Regex}
let re = Regex("[,;\\s]+")
let parts = re.split("one, two; three four")
for _, p in parts {
print(p)
}