data Regex
Regular expression (§1.5, D-1802, D-3140); re"…" literals have this type. PCRE2 syntax in Unicode mode (\w, \d, case
folding and \p{..} are Unicode-aware; lookaround, named groups (?<name>…), lazy quantifiers, (?i)).
matches tests the WHOLE string; find, findAll, captures, replace and split search inside it.
Example:
fn main() io {
let re = Regex.compile("(?<year>[0-9]{{4}})-(?<month>[0-9]{{2}})")!
match re.captures("released 2026-11") {
c? => print("{c.text(0)!} {c.named("year")!.text} {c.named("month")!.text}")
none => print("no match")
}
}Вывод
2026-11 2026 11
pub(api) data RegexСложность. matching is linear in the length of the text for patterns without backtracking-heavy constructs.
Потокобезопасность. a compiled pattern is immutable; safe to share between tasks.
С версии. 1.0.0-beta
Поля
pub(api) pattern: str | The pattern text (PCRE2 syntax). |
Методы
compile | (pattern: str) -> Regex | ParseErrorCompiles at run time; prefer the literal re"…", which is checked when the program is built. |
matches | (s: str) -> boolTrue when the whole of s matches the pattern. |
isMatch | (s: str) -> boolTrue when the pattern matches anywhere in s. |
find | (s: str) -> Match?The first match. |
findAll | (s: str) -> List<Match>Every non-overlapping match, left to right (an empty match advances by one character). |
captures | (s: str) -> Captures?The first match with its groups. |
capturesAll | (s: str) -> List<Captures>Every match with its groups. |
replace | (s: str, with: str) -> strReplaces the first match; $1, ${name}, $0 and $$ in with refer to groups (unknown groups expand to nothing). |
replaceAll | (s: str, with: str) -> strReplaces every match. |
split | (s: str) -> List<str>Pieces between the matches (re"\s*,\s*" splits a comma list); empty matches do not split. |
Определено в: std/core/text.oxl:1097