match_replace

Definition: sequence res = match_replace(object needle, sequence haystack, object replacement, integer max=0)
Description: Finds a "needle" in a "haystack", and replace any, or only the first few, occurrences with a replacement.

needle: an object to search and perhaps replace.
haystack: a sequence to be inspected.
replacement: an object to substitute for instances of needle.
max: number of items to replace, or 0 to replace all occurrences

Returns a sequence, the modified haystack.
Comments: Replacements will not be made recursively on the part of haystack that was already changed.

If max is 0 or less, any occurrence of needle in haystack will be replaced by replacement.
Otherwise, only the first max occurrences are replaced.

If either needle or replacement are atoms they are treated as if you had passed in a length-1 sequence containing the said atom.

This routine is defined in builtins/matchrepl.e and is provided only for compatibility with OpenEuphoria.
Example:
s = match_replace("the", "the cat ate the food under the table", "THE", 0)  -- s is "THE cat ate THE food under THE table"
s = match_replace("the", "the cat ate the food under the table", "THE", 2)  -- s is "THE cat ate THE food under the table"
s = match_replace('/', "/euphoria/demo/unix", '\\', 2)                      -- s is "\\euphoria\\demo/unix"