Regex module

replace_n

procedureextension2 overloadsSource

Replaces the first limit matches (all of them when limit < 0).

Example↗
Regex :: #import "Extensions/Regex";

re, _ := Regex.compile("[0-9]+");
assert(Regex.replace_n(*re, "1 2 3", "#", 2) == "# # 3");
Regex.free_regex(*re);
replace_n :: (
    re: *Regex,
    text: string,
    replacement: string,
    limit: int
) -> string

Replaces the first limit matches (all of them when limit < 0).

Parameters

re: *Regex
text: string
replacement: string
the new text; $1 or ${1} stands for a group and $name or ${name} for a named one.
limit: int
how many matches to replace. Negative replaces all of them.

Returns

string
Example↗
Regex :: #import "Extensions/Regex";

re, _ := Regex.compile("[0-9]+");
assert(Regex.replace_n(*re, "1 2 3", "#", 2) == "# # 3");
Regex.free_regex(*re);
replace_n :: ($R: Type, text: string, replacement: string, limit: int) -> string

Replaces the first limit matches of the static pattern; negative replaces all.

Parameters

$R: Type
text: string
replacement: string
limit: int

Returns

string
Example↗
Regex :: #import "Extensions/Regex";
Digits :: Regex.Static("[0-9]+");

assert(Regex.replace_n(Digits, "1 2 3", "#", 2) == "# # 3");

See also