Re: match_replace for every instance forever
- Posted by mattlewis (admin) Nov 08, 2011
- 1299 views
euphoric said...
I want to turn all multiple space sequences in a string to a single space.
I'd do it with a slightly modified version of the std routine match_replace:
public function match_replace_all(object needle, sequence haystack, object replacement ) integer posn integer needle_len integer replacement_len integer scan_from integer cnt if max < 0 then return haystack end if cnt = length(haystack) if max != 0 then cnt = max end if if atom(needle) then needle = {needle} end if if atom(replacement) then replacement = {replacement} end if needle_len = length(needle) - 1 replacement_len = length(replacement) scan_from = 1 while posn with entry do haystack = replace(haystack, replacement, posn, posn + needle_len) cnt -= 1 if cnt = 0 then exit end if scan_from = posn + 1 -- <--- this is the change. Used to be "+ replacement_len" entry posn = match(needle, haystack, scan_from) end while return haystack end function
Matt