std.regex
Regular expressions: a Regex says whether a text has a pattern, finds where, with the groups between parentheses, replaces and splits. The time of a search grows with the length of the text and never more, whatever the expression and the text: an expression that comes from outside cannot stop a service.
import std.regex
regex.Regex date = regex.Regex("(\\d{4})-(\\d{2})-(\\d{2})")
regex.Match found
if (date.find("released on 2026-11-12", ref found)) {
println("{} of the year {}", found.text(), found.group(1)) // 2026-11-12 of the year 2026
}
println("{}", date.replace("2026-11-12", "$3/$2/$1")) // 12/11/2026The guide: Regular expressions.
Generata dalla documentazione nei sorgenti della libreria, che è in inglese.
#Tipi
#Regex
struct RegexA regular expression, ready to search. Making one reads the expression, so a program makes it once and keeps it; it can be copied and given to the tasks.
The syntax is the one of RE2, which Go and Rust use: . any character but the end of line [a-z0-9_] one of a class [^a-z] not of it \d a digit \w a letter, a digit or _ \s a space \D \W \S not of them [[:alpha:]] and the other classes of POSIX, inside a class x* x+ x? x{3} x{2,} x{2,5} as many as it can; x*? x+? x?? x{2,5}? as few ab one after the other a|b one or the other (x) a group (?:x) not counted (?P\<name>x) or (?\<name>x) with a name ^ $ the start and the end of the text; of each line with (?m) \A \z the start and the end of the text \b \B at the edge of a word, or not (?i) capitals or not (?s) the . takes the end of line too (?m) (?i:x) only for x \n \t \r \f \v \a \x41 \x{1F600} \Q...\E as it is \. \* \\ the character itself The characters are the ones of Unicode, so . takes "è" whole; \d, \w, \s, \b and (?i) know the letters of ASCII only. There are no references to a group (\1) and no lookahead or lookbehind ((?=x), (?!x), (?\<=x)), which need more than a time that grows with the text; nor the classes of Unicode (\p{L}).
Regex(string pattern)Reads the expression. It throws an Error that says what is wrong and where for an expression that is not valid, or too large.
#Regex.pattern
string pattern() constThe expression as it was written.
#Regex.groups
i64 groups() constHow many groups the expression has, without the whole match, the group 0.
#Regex.matches
bool matches(string text) constTrue when the text has the expression somewhere in it.
#Regex.matchesWhole
bool matchesWhole(string text) constTrue when the whole text is the expression, from its start to its end.
#Regex.find
bool find(string text, ref Match found) constFinds the first match of the expression in the text, the leftmost one: true when there is one, which goes into found with its groups.
#Regex.findFrom
bool findFrom(string text, i64 from, ref Match found) constFinds the first match that starts at the position from, in bytes, or after it. The text before from is still there for ^, \b and the others.
#Regex.findAll
list<Match> findAll(string text) constAll the matches of the expression in the text, in order, each one after the end of the one before.
#Regex.replace
string replace(string text, string replacement) constThe text with every match replaced by replacement, where $1 is the group 1 of the match, ${1} the same, ${name} the group with that name, $0 the whole match and $$ a $. It throws an Error for a group that the expression does not have.
#Regex.replaceFirst
string replaceFirst(string text, string replacement) constThe text with the first match replaced, as replace() does.
#Regex.split
list<string> split(string text) constThe pieces of the text between the matches: Regex("\\s*,\\s*").split("a , b,c") gives "a", "b" and "c". A text with no match is one piece.
#Match
struct MatchA match of an expression in a text, from find() and findAll(): where it is, its text and the ones of its groups. The positions are in bytes, as the ones of a string. A Match that no search has filled has no text, and -1 as its start and its end.
#Match.start
i64 start() constWhere the match starts in the text.
#Match.end
i64 end() constWhere the match ends: the position after its last byte.
#Match.text
string text() constThe text of the match.
#Match.group
string group(i64 index) constThe text of the group index: 1 is the first ( of the expression, 0 the whole match. Empty for a group that the match did not go through, as (b) in "a|(b)" on "a": has() tells it from a group that took an empty text. A group that the expression does not have is a panic.
#Match.has
bool has(i64 index) constTrue when the match went through the group index.
#Match.groupStart
i64 groupStart(i64 index) constWhere the group index starts, or -1 when the match did not go through it.
#Match.groupEnd
i64 groupEnd(i64 index) constWhere the group index ends, or -1 when the match did not go through it.
#Match.named
string named(string name) constThe text of the group with a name, as (?P\<year>\d+): empty for a group that the match did not go through. A name that the expression does not have is a panic.
#Funzioni
#escape
string escape(string text)The text with a \ before the characters that mean something in an expression: an expression that finds the text as it is. escape("1+1") is "1\+1".