Regex.find
func Regex.find(p: Regex.Pattern, s: string): Regex.Match | nullRegex.find returns the leftmost match of p in s, or null when there is none. Of the matches that start there, the one taken is the one a backtracking matcher finds first, as in Perl and JavaScript: alternatives are tried left to right, greedy repetition prefers more and lazy repetition fewer.
Details
startandendcount code points, not bytes, soString.slice(s, m.start, m.end)ism.text.A pattern that can match the empty string, such as
/x*/, matches at position 0 of any string, so the result is an empty match rather thannull.Of the matches that start at the leftmost position, the first one a backtracking matcher finds wins, as in Perl and JavaScript, not the longest:
antRegex.find(/a|ab/, "ab")?.text // "a"
Basic examples
Find the first run of digits:
Regex.find(/\d+/, "ab12") // Regex.Match{ text: "12", start: 2, end: 4, groups: ["12"], named: Map{} }No match gives null:
Regex.find(/\d+/, "abc") // nullRead the matched text with ?., and give a fallback with ??:
Regex.find(/\d+/, "room 42")?.text ?? "none" // "42"
Regex.find(/\d+/, "lobby")?.text ?? "none" // "none"Scope
Only the first match is returned, even when there are more:
Regex.find(/\d+/, "1 22 333")?.text // "1"A group's text is in groups, after the whole match at index 0:
Regex.find(/(\w+)@(\w+)/, "mail ann@home")?.groups // ["ann@home", "ann", "home"]A group that took no part in the match is null:
Regex.find(/(a)|(b)/, "b")?.groups // ["b", null, "b"]
Regex.find(/(x)?y/, "y")?.groups // ["y", null]A named group is also in named, in the pattern's order:
let m = Regex.find(/(?<year>\d{4})-(?<month>\d\d)/, "since 2026-11")
m?.named // Map{ "year": "2026", "month": "11" }
m?.named["month"] // "11"An empty string has no match for a pattern that needs a character:
Regex.find(/\d+/, "") // nullIndices count code points, so é and 🐜 each take one position:
Regex.find(/ant/, "café 🐜 ant") // Regex.Match{ text: "ant", start: 7, end: 10, groups: ["ant"], named: Map{} }Flags change how the pattern matches: i ignores case, m lets ^ match after a line break, and s lets . match a line break:
Regex.find(/ANT/i, "an Ant")?.text // "Ant"
Regex.find(/^b/m, "a\nb")?.start // 2
Regex.find(/^b/, "a\nb") // null
Regex.find(/a.b/s, "a\nb")?.text // "a\nb"
Regex.find(/a.b/, "a\nb") // nullA pattern built from data with Regex.compile works the same as a literal:
let p = Result.unwrap(Regex.compile("a.c"))
Regex.find(p, "xabc")?.text // "abc"The pattern comes first, so a call through . goes on the pattern:
/\d+/.find("a 7")?.text // "7"Applications
Parse a version number out of a line of text, with a guard for the line that has none:
type Version = { major: int, minor: int, patch: int }
func parseVersion(s: string): Version | null {
let m = Regex.find(/(\d+)\.(\d+)\.(\d+)/, s)
if m == null { return null }
let n = m.groups.map({ x?.toInt() ?? 0 })
Version{ major: n[1], minor: n[2], patch: n[3] }
}
parseVersion("ant version 0.1.0 (darwin)") // Version{ major: 0, minor: 1, patch: 0 }
parseVersion("dev build") // nullSplit a key=value setting with named groups, and return an error for a line that is not one:
type Setting = { key: string, value: string }
func parseSetting(line: string): Result[Setting, Error] {
let m = Regex.find(/^\s*(?<key>\w+)\s*=\s*(?<value>.*)$/, line)
if m == null {
return Result.fail("not a setting: ${line}")
}
Result.ok(Setting{ key: m.named["key"] ?? "", value: m.named["value"] ?? "" })
}
parseSetting(" port = 8080") // Ok{ value: Setting{ key: "port", value: "8080" } }
parseSetting("# comment") // Err{ error: Error{ message: "not a setting: # comment" } }Cut a string at the first match, using start and end:
func splitFirst(s: string, sep: Regex.Pattern): Array[string] {
let m = Regex.find(sep, s)
if m == null { return [s] }
[s.slice(0, m.start), s.slice(m.end, s.length())]
}
splitFirst("name: Ann: admin", /:\s*/) // ["name", "Ann: admin"]
splitFirst("plain", /:\s*/) // ["plain"]Properties and relations
text is the slice of s between start and end, and is groups[0]:
let s = "price: 12 €, or 30 €"
let m = Regex.find(/\d+ €/, s)
assert(m != null)
assert(s.slice(m.start, m.end) == m.text)
assert(m.groups[0] == m.text)Regex.find gives null exactly when Regex.test is false:
let p = /[aeiou]/
for s in ["rhythm", "ant", ""] {
assert((Regex.find(p, s) == null) == !Regex.test(p, s))
}It returns the first match Regex.findAll returns:
let p = /\d+/
let s = "a1b22c333"
assert(Regex.find(p, s) == Regex.findAll(p, s).first())Possible issues
The result may be null, so reading a field without narrowing is a compile error:
let m = Regex.find(/\d+/, "x 42")
let digits = m.text // error: Regex.Match | null has no field text: narrow the value firstThe pattern comes first, so the call goes on the pattern, not the string:
"a 7".find(/\d+/) // error: string has no field or function find/\d+/.find("a 7")?.text // "7"A pattern that matches the empty string finds an empty match at the start instead of null. Use + instead of * to require at least one character:
Regex.find(/\d*/, "abc 12")?.text // ""
Regex.find(/\d+/, "abc 12")?.text // "12"\w, \d, and \s match ASCII only, so \w+ stops at an accented letter. Use \pL for letters in any script:
Regex.find(/\w+/, "héllo")?.text // "h"
Regex.find(/\pL+/, "héllo")?.text // "héllo"See also
Regex.findAll, Regex.test, Regex.replaceWith, Regex.compile, Regex.Match, Regex.Pattern, String.slice, String.indexOf
