std.regex.replaceAll
- multiple declarations
Function replaceAll
Construct a new string from input
by replacing all of the
fragments that match a pattern re
with a string generated
from the match according to the format
specifier.
R replaceAll(R, C, RegEx)
(
R input,
RegEx re,
const(C)[] format
) @trusted
if (isSomeString!R && is(C : dchar) && isRegexFor!(RegEx, R));
To replace only the first match use replaceFirst
.
Parameters
Name | Description |
---|---|
input | string to search |
re | compiled regular expression to use |
format | format string to generate replacements from, see the format string. |
Returns
A string of the same type as input
with the all
of the matches (if any) replaced.
If no match is found returns the input string itself.
Example
// insert comma as thousands delimiter
auto re = regex(r"(?<=\d)(?=(\d\d\d)+\b)","g");
writeln(replaceAll("12000 + 42100 = 54100", re, ",")); // "12,000 + 42,100 = 54,100"
Function replaceAll
This is a general replacement tool that construct a new string by replacing
matches of pattern re
in the input
. Unlike the other overload
there is no format string instead captures are passed to
to a user-defined functor fun
that returns a new string
to use as replacement.
R replaceAll(alias fun, R, RegEx)
(
R input,
RegEx re
) @trusted
if (isSomeString!R && isRegexFor!(RegEx, R));
This version replaces all of the matches found in input
,
see replaceFirst
to replace the first match only.
Returns
A new string of the same type as input
with all matches
replaced by return values of fun
. If no matches found
returns the input
itself.
Parameters
Name | Description |
---|---|
input | string to search |
re | compiled regular expression |
fun | delegate to use |
Example
string baz(Captures!(string) m)
{
import std .string : toUpper;
return toUpper(m .hit);
}
// Capitalize the letters 'a' and 'r':
auto s = replaceAll!(baz)("Strap a rocket engine on a chicken.",
regex("[ar]"));
writeln(s); // "StRAp A Rocket engine on A chicken."
Authors
Dmitry Olshansky,
API and utility constructs are modeled after the original std
by Walter Bright and Andrei Alexandrescu.