std.regex.replace_first
- multiple declarations
- Function replaceFirst
- Function replaceFirst
Function replaceFirst
Construct a new string from
by replacing the first input
match
with
a string generated from it according to the
specifier.
format
To replace
all matches use replaceAll
.
Prototype
R replaceFirst(R, C, RegEx)( R input, RegEx re, const(C)[] format ) 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 with the first match
(if any) replaced.
If no match
is found returns the input
string itself.
Example
assert(replaceFirst("noon", regex("n"), "[$&]") == "[n]oon");
Function replaceFirst
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 the first match
in
,
see input
replaceAll
to replace
the all of the matches.
Prototype
R replaceFirst(alias fun, R, RegEx)( R input, RegEx re ) 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
Example
string list = "#21 out of 46";
string newList = replaceFirst!(cap => to!string(to!int(cap.hit)+1))
(list, regex([0-9]+
));
assert(newList == "#22 out of 46");
Authors
Dmitry Olshansky,
API and utility constructs are modeled after the original
by Walter Bright and Andrei Alexandrescu.
std.regex