The OpenD Programming Language

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.

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

  1. R replaceFirst(R input, RegEx re, const(C)[] format)
  2. R replaceFirst(R input, RegEx re)
    R
    replaceFirst
    (
    alias fun
    R
    RegEx
    )
    (,
    RegEx re
    )
    if (
    isRegexFor!(RegEx, R)
    )

Return Value

Type: R

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.

Examples

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]+`));
assert(newList == "#22 out of 46");

Meta