C 393
Edits:
- Thanks to @algorithmshark for lots of help making the original shorter
- Thanks to @ceilingcat for some very nice pieces of golfing - now even shorter
Here is the code:
#import<bits/stdc++.h>
#define M(x)bzero(x,99);
#define P o[i])
#define N(x)P;else if(n<x)(P==92?
#define O (o[++i]):(P==47?n++:
using S=std::string;char p[99],*q=p,r[99],*s=r;int i,t;main(int n,char**m){for(S o=m[1];i<=o.size();++i){if(!N(3)putchar O putchar(N(4)*q++=O(*q++=N(5)*s++=O(*s++=P;if(n>4){for(;(t=o.find(p,i+1))-S::npos;)o=o.substr(0,t)+r+o.substr(t+strlen(p));M(q=p)M(s=r)n=2;}}}