Julia, 93 bytes
f(s,p,r,R=Regex(join("^$p\$","(.*?)")))=ismatch(R,s)?join([match(R,s).captures [r...""]']'):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,R=Regex(join("^$p\$","(.*?)")))=
ismatch(R,s)?join([match(R,s).captures [r...""]']'):s
f (generic function with 2 methods)
julia> f("aacbcbabcccaabcbabcaabbbbca","abaaaccbac","1223334444")
"aacbcbabcccaabcbabcaabbbbca"
julia> f("aacbcbabcccaabcbabcaabbbbcac","abaaaccbac","1223334444")
"1ac2cb2bccc33b3bab4aa4bbbc44"