Skip to main content
1 of 3
Dennis
  • 210.6k
  • 41
  • 370
  • 825

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"
Dennis
  • 210.6k
  • 41
  • 370
  • 825