Julia, 93 9090 86 bytes
f(s,p,r,m=match)=(try s=join([match(Regex(join("^$p\$","(.*?)")),s))=m==nothing?s:join([m.captures';[r...""]])end;s)
Having to test separately if the match was successful kinda destroys the score. A substitution would require casting to Base.SubstitutionString
, which probably isn't worth it...
Test run
julia> f(s,p,r,m=match)=(try s=join([match(Regex(join("^$p\$","(.*?)")),s))=
m==nothing?s:join([m.captures';[r...""]])end;s)
f (generic function with 21 methodsmethod)
julia> f("aacbcbabcccaabcbabcaabbbbca","abaaaccbac","1223334444")
"aacbcbabcccaabcbabcaabbbbca"
julia> f("aacbcbabcccaabcbabcaabbbbcac","abaaaccbac","1223334444")
"1ac2cb2bccc33b3bab4aa4bbbc44"