cromo The Cromo Guide
Download PDF IT

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.

cromo
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/2026

The guide: Regular expressions.

Made from the documentation in the sources of the library.

#Types

#Regex

cromo
struct Regex

A 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}).

cromo
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

cromo
string pattern() const

The expression as it was written.

#Regex.groups

cromo
i64 groups() const

How many groups the expression has, without the whole match, the group 0.

#Regex.matches

cromo
bool matches(string text) const

True when the text has the expression somewhere in it.

#Regex.matchesWhole

cromo
bool matchesWhole(string text) const

True when the whole text is the expression, from its start to its end.

#Regex.find

cromo
bool find(string text, ref Match found) const

Finds 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

cromo
bool findFrom(string text, i64 from, ref Match found) const

Finds 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

cromo
list<Match> findAll(string text) const

All the matches of the expression in the text, in order, each one after the end of the one before.

#Regex.replace

cromo
string replace(string text, string replacement) const

The 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

cromo
string replaceFirst(string text, string replacement) const

The text with the first match replaced, as replace() does.

#Regex.split

cromo
list<string> split(string text) const

The 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

cromo
struct Match

A 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

cromo
i64 start() const

Where the match starts in the text.

#Match.end

cromo
i64 end() const

Where the match ends: the position after its last byte.

#Match.text

cromo
string text() const

The text of the match.

#Match.group

cromo
string group(i64 index) const

The 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

cromo
bool has(i64 index) const

True when the match went through the group index.

#Match.groupStart

cromo
i64 groupStart(i64 index) const

Where the group index starts, or -1 when the match did not go through it.

#Match.groupEnd

cromo
i64 groupEnd(i64 index) const

Where the group index ends, or -1 when the match did not go through it.

#Match.named

cromo
string named(string name) const

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

#Functions

#escape

cromo
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".