Pike v8.1 release 6

Method Regexp.PCRE.Plain()->replace()


Method replace

string replace(string subject, string|function(:void) with, mixed|void ... args)

Description

replace all occurances of a pattern in a subject; callbacks and replacements will be from the first occurance, not from the last as in Regexp.Builtin.replace.

if with is a function, the first argument will be the total match string, and the subsequent arguments will contain any submatches

example:

> Regexp.PCRE("b[^-]*m")->replace("abam-boom-fooabadoom","gurka");
Result: "agurka-gurka-fooagurka"
> Regexp.PCRE("b[^-]*m")->replace("abam-boom-fooabadoom",
     lambda(string s) { werror("%O\n",s); return "gurka"; });
"bam"
"boom"
"badoom"
Result: "agurka-gurka-fooagurka"

example:

> Regexp.PCRE("([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})")
  ->replace("foo@bar.org",
    lambda(string whole, string user, string loc, string domain)
      { return user + " from " + loc + " dot " + domain; }
   );
(4) Result: "foo from bar dot org"