Regex.replaceFirst
func Regex.replaceFirst(p: Regex.Pattern, s: string, replacement: string): stringRegex.replaceFirst returns s with the first match of p, the first that Regex.findAll returns, replaced by replacement, expanded as in Regex.replace.
Details
- Only the leftmost match is replaced; the rest of
scomes back as it was. - When
pdoes not match, the result equalss. replacementexpands$0(the whole match),$1,${1},${name}, and$$. A group that took no part, or that does not exist, expands to"".- In a
"..."string,${interpolates. Write"\${1}"or a backtick string such as`${1}`.
Basic examples
Replace the first run of digits:
Regex.replaceFirst(/\d+/, "a1b22c333", "#") // "a#b22c333"Rearrange the groups of the first match only:
Regex.replaceFirst(/(\w+)@(\w+)/, "ann@home bob@work", "$2:$1") // "home:ann bob@work"When nothing matches, the string comes back unchanged:
Regex.replaceFirst(/x/, "abc", "y") // "abc"Scope
Named groups expand with ${name}. A backtick string keeps ${ from interpolating:
Regex.replaceFirst(/(?<user>\w+)@(?<host>\w+)/, "ann@home bob@work", `${host}/${user}`) // "home/ann bob@work"$0 is the whole match:
Regex.replaceFirst(/\d+/, "a1b22", "[$0]") // "a[1]b22"$$ writes one $:
Regex.replaceFirst(/\d+/, "cost 5", "$$") // "cost $"
Regex.replaceFirst(/\$/, "a$b", "$$$$") // "a$$b"A group that took no part expands to "":
Regex.replaceFirst(/(a)|(b)/, "b", "[$1]") // "[]"A pattern that matches the empty string replaces at the start of s, and ^ inserts a prefix:
Regex.replaceFirst(/x*/, "abc", "-") // "-abc"
Regex.replaceFirst(/^/, "abc", "> ") // "> abc"An empty s gives "", unless the pattern matches the empty string:
Regex.replaceFirst(/a/, "", "b") // ""
Regex.replaceFirst(/x*/, "", "b") // "b"Flags apply as in any match. With i, the first É matches é:
Regex.replaceFirst(/é/i, "CAFÉ café", "e") // "CAFe café"Unicode text works like any other:
Regex.replaceFirst(/🐜/, "🐜🐝🐜", "🐞") // "🐞🐝🐜"Call it through . on a pattern, or with named arguments:
let digits = /\d+/
digits.replaceFirst("a1b22", "#") // "a#b22"
Regex.replaceFirst(p: /o/, s: "foo", replacement: "0") // "f0o"Applications
Reformat the first date in a log line and leave later dates alone:
let line = "2026-11-12 saved 2026-11-01 backup"
Regex.replaceFirst(/(\d{4})-(\d{2})-(\d{2})/, line, "$3.$2.$1") // "12.11.2026 saved 2026-11-01 backup"Bump the first version line of a config file, not the one in a later section:
let config = "name = \"app\"\nversion = \"1.2.3\"\n[dep]\nversion = \"0.9.0\"\n"
print(Regex.replaceFirst(/(?m)^version = ".*"$/, config, `version = "1.3.0"`))name = "app"
version = "1.3.0"
[dep]
version = "0.9.0"Strip leading whitespace, which only the first match can be:
Regex.replaceFirst(/^\s+/, " hi there", "") // "hi there"Properties and relations
When p matches, the result is the text before the first match, the expanded replacement, and the text after it, as Regex.find locates it:
let s = "x=1, y=22"
let m = Regex.find(/\d+/, s)
assert(m != null)
let expected = s.slice(0, m.start) + "#" + s.slice(m.end, s.length())
assert(Regex.replaceFirst(/\d+/, s, "#") == expected)When p does not match, s comes back unchanged:
let s = "no digits"
assert(!Regex.test(/\d/, s))
assert(Regex.replaceFirst(/\d/, s, "#") == s)When p matches at most once, it agrees with Regex.replace:
for s in ["id: 42", "id: none", ""] {
assert(Regex.findAll(/\d+/, s).length() <= 1)
assert(Regex.replaceFirst(/\d+/, s, "N") == Regex.replace(/\d+/, s, "N"))
}Possible issues
$1x reads the group name 1x, which does not exist, so it expands to "". Write ${1}x:
Regex.replaceFirst(/(\w+)/, "ab cd", "$1x") // " cd"
Regex.replaceFirst(/(\w+)/, "ab cd", "\${1}x") // "abx cd"In a "..." string, ${...} interpolates before Regex.replaceFirst sees it:
let host = "?"
Regex.replaceFirst(/(?<host>\w+)/, "ann", "${host}") // "?"
Regex.replaceFirst(/(?<host>\w+)/, "ann", `${host}`) // "ann"The pattern comes first, so a string cannot be the receiver of a dot call or the start of a pipe. On a string, .replaceFirst is String.replaceFirst:
let s = "a.b.c"
let fixed = s.replaceFirst(/\./, "!") // error: cannot use Regex.Pattern where string is expectedIn a pattern, . matches any character, so /./ replaces the first character. Escape it as \., or use String.replaceFirst to replace a fixed string. Regex.escape does the escaping for a string from data:
Regex.replaceFirst(/./, "a.b", "!") // "!.b"
Regex.replaceFirst(/\./, "a.b", "!") // "a!b"
"a.b".replaceFirst(".", "!") // "a!b"See also
Regex.replace · Regex.replaceWith · Regex.find · Regex.escape · String.replaceFirst
