MOO-cows Mailing List Archive


Re: regex substitution

I see what you want.  I wrote a #19845:grep_and_replace @LambdaMOO to handle
regular expression replacement in an object or group of objects.  It didn't,
however, handle individual strings.  Here's one that does:

@verb me:replace_regex tnt "Rxd" ME
@prog me:replace_regex
":replace_regex(string, expression, replacement)";
s = args[1];
e = args[2];
r = args[3];
"{s, e, r} = args;  // I haven't even used 1.8 yet.  MMM CANNA WAIT.";
MAXIT = 50;
while ((MAXIT = MAXIT - 1) && (m = match(s, e)))
  s[m[3][1][1]..m[3][1][2]] = r;
return s;

;player:replace_regex("Can you be zzzomewhat rezzzpectful?", "%(zz+%)", "s")
=> "Can you be somewhat respectful?"
[used 47 ticks, 1 second.]

I haven't performed any thorough tests, but this at least handles global
replacement.  You could modify it to accept replacement lists to allow for
replacing more than one expression in a run, but it'd really tangle the code.

-   (corp)
-  (self)

Home | Subject Index | Thread Index