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