Python 3.5, 87 bytes
import re
lambda s,p,r:re.sub('(.*?)'.join(p),'\g<%d>'.join(r)%(*range(1,len(r)),),s,1)
repl.it to verify all test cases.
How it works
'(.*?)'.join(p)
builds a search pattern that matches the subsequence to be replaced and anything between its elements.Since the modifiersquantifiers are lazy, each
(.*?)
will match as few characters as possible.For the pattern
ghost
, the constructed regex isg(.*?)h(.*?)o(.*?)s(.*?)t
.'\g<%d>'.join(r)%(*range(1,len(r)),)
builds the replacement string, using string formatting.Each
\g<n>
refers to the nth captured group, just like\n
would.For the replacement
12345
, the constructed string is1\g<1>2\g<2>3\g<3>4\g<4>5
.re.sub(...,...,s,1)
performs at most one replacement in the strings
.