Types
Match info from std.regex.find. No match is start = end = -1 with empty text; offsets are byte positions into the searched string.
text: string | The matched text, or |
|---|---|
start: int | Byte offset the match starts at, or -1. |
end: int | Byte offset one past the match, or -1. |
Functions
Reports whether the pattern compiles successfully.
pattern: Pattern to compile.
println(std.regex.is_valid("[a-z]+")) // true
println(std.regex.is_valid("[a-z")) // falseReports whether the pattern matches any part of the text.
pattern: Pattern to apply.text: Text to search.
println(std.regex.is_match("^[a-z]+[0-9]+$", "moon42")) // trueReturns the first match.
pattern: Pattern to apply.text: Text to search.
let m = std.regex.find("[0-9]+", "page 42 of 96")
println(m.text) // 42
println(m.start) // 5
println(m.end) // 7
println(std.regex.find("[0-9]+", "moon").start) // -1Searches from a byte offset in the original text. Anchors and lookbehind retain the full subject as context. Negative offsets and offsets beyond the subject return the no-match sentinel; an offset at the end can match an empty suffix.
Returns every non-overlapping match.
pattern: Pattern to apply.text: Text to search.
println(std.regex.find_all("[0-9]+", "page 42 of 96")) // ["42", "96"]Returns the first match and its capture groups.
pattern: Pattern to apply.text: Text to search.
let groups = std.regex.captures("([a-z]+)-([0-9]+)", "release moon-42 today")
println(groups) // ["moon-42", "moon", "42"]Returns capture groups for the first match at or after a byte offset, preserving the original subject for anchors and lookbehind. The whole match is group zero; unmatched groups are empty strings. Invalid patterns, invalid offsets and absent matches return an empty array.
Returns capture names indexed by group number. Group zero and unnamed groups have empty names. Invalid patterns return an empty array.
Replaces the first matching occurrence.
pattern: Pattern to apply.text: Text to rewrite.replacement: Text put in place of the match.
println(std.regex.replace("[0-9]+", "v1 v22", "#")) // v# v22
println(std.regex.replace_all("[0-9]+", "v1 v22", "#")) // v# v#Replaces every matching occurrence.
pattern: Pattern to apply.text: Text to rewrite.replacement: Text put in place of every match.
// "$1" in the replacement inserts the first capture group.
let out = std.regex.replace_all("([a-z]+)@[a-z]+", "mail moon@lang now", "$1")
println(out) // mail moon nowSplits the input at the requested separators.
pattern: Pattern that separates the pieces.text: Text to divide.
println(std.regex.split(",+", "a,b,,c")) // ["a", "b", "c"]