std.regex

Regular-expression matching, captures, and replacement.

Importuse std.regex

Types

struct Match#

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 "" when nothing matched.

start: int

Byte offset the match starts at, or -1.

end: int

Byte offset one past the match, or -1.

Functions

extern fn is_valid(pattern: string) -> bool#

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"))   // false
extern fn is_match(pattern: string, text: string) -> bool#

Reports 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")) // true
extern fn find(pattern: string, text: string) -> Match#

Returns 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) // -1
extern fn find_from(pattern: string, text: string, from: int) -> Match#

Searches 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.

extern fn find_all(pattern: string, text: string) -> [string]#

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"]
extern fn captures(pattern: string, text: string) -> [string]#

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"]
extern fn captures_from(pattern: string, text: string, from: int) -> [string]#

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.

extern fn capture_names(pattern: string) -> [string]#

Returns capture names indexed by group number. Group zero and unnamed groups have empty names. Invalid patterns return an empty array.

extern fn replace(pattern: string, text: string, replacement: string) -> string#

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#
extern fn replace_all(pattern: string, text: string, replacement: string) -> string#

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 now
extern fn split(pattern: string, text: string) -> [string]#

Splits 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"]