View source code
Display the source code in std/regex.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

std.regex.replaceFirst - multiple declarations

Function replaceFirst

Construct a new string from input by replacing the first match with a string generated from it according to the format specifier.

R replaceFirst(R, C, RegEx) (
  R input,
  RegEx re,
  const(C)[] format
)
if (isSomeString!R && is(C : dchar) && isRegexFor!(RegEx, R));

To replace all matches use replaceAll.

Parameters

NameDescription
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 with the first match (if any) replaced. If no match is found returns the input string itself.

Example

writeln(replaceFirst("noon", regex("n"), "[$&]")); // "[n]oon"

Function replaceFirst

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 replaceFirst(alias fun, R, RegEx) (
  R input,
  RegEx re
)
if (isSomeString!R && isRegexFor!(RegEx, R));

This version replaces the first match in input, see replaceAll to replace the all of the matches.

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.

Example

import std.conv : to;
string list = "#21 out of 46";
string newList = replaceFirst!(cap => to!string(to!int(cap.hit)+1))
    (list, regex(`[0-9]+`));
writeln(newList); // "#22 out of 46"

Authors

Dmitry Olshansky,

API and utility constructs are modeled after the original std.regex by Walter Bright and Andrei Alexandrescu.

License

Boost License 1.0.