I don't know if this answer will be useful for because they deviate from the path you chose. But, if I understood your question correctly, I think this is easier than replacing text inside a macro. This is how I set things up in a class I wrote.
First some preliminaries. Nothing interesting here:
\catcode`\@=11%
\def\projectname{test}%
\input docstrip.tex
\keepsilent
\askforoverwritefalse
Now we'll set up the environment to make the definition of the preambles/postambles. We'll make \^^M active so when we pass that to docstrip's preamble/postamble system it will manage to add the \MetaPrefix:
\begingroup
\catcode`\^^M=\active%
\obeyspaces%
(I used \obeyspaces to allow alignment of text spanning multiple lines, like in the \listoffiles macro below)
Now we (g)define whichever preamble/postamble text we want:
\gdef\copyright{%
\ds@heading% This is file... generated with the docstrip utility.
\ReferenceLines% The original source files were: ...
This is a generated file.
Copyright 2019 by Phelype Oleinik
}%
\gdef\lppllicense{%
This work may be distributed and/or modified under the conditions of the
LaTeX Project Public License, either version 1.3c of this license or (at
your option) any later version. The latest version of this license is in
http://www.latex-project.org/lppl.txt
and version 1.3c or later is part of all distributions of LaTeX version
2005/12/01 or later.
}%
\gdef\listoffiles{%
This work consists of the files \projectname.dtx, and
\projectname.ins,
and the derived files \projectname.cls,
README.txt.%
}%
\endgroup
You can split these in multiple parts, and later you can concatenate them. For example, I have a \copyright and a \lppllicense, later I do \def\thispreamble{\copyright\lppllicense} to join them for a given file.
Now that we defined the pieces of text we'll use we can \generate the files using that. For instance, the class (or package, or whatever you're doing) file can be generated with:
\generate
{%
\file{\projectname.cls}%
{%
% Joining \copyright and \lppllicense
\def\thispreamble{\copyright\lppllicense}%
% Getting docstrip to use that
\usepreamble\thispreamble
\usepostamble\listoffiles
\from{\projectname.dtx}{class}%
}%
}%
here the \MetaPrefix (currently %%) is inserted at every line, so everything will go commented to your class/package file.
What if I want a readme file without %%? You can simply \def\MetaPrefix{}. But this inserts, by default, a leading space, so we can make \MetaPrefix remove a space defining it with an argument: \def\MetaPrefix#1{}.
\generate
{%
\file{README.txt}%
{%
\def\MetaPrefix#1{}% Remove MetaPrefix for readme
\def\thispreamble{\copyright\lppllicense}%
\usepreamble\thispreamble
\usepostamble\listoffiles
\from{\projectname.dtx}{readme}%
}%
}%
If you had, for instance, some Lua code as well you could \def\MetaPrefix{--}:
\generate
{%
\file{luacode.lua}%
{%
\def\MetaPrefix{--}%
\def\thispreamble{\copyright\lppllicense}%
\usepreamble\thispreamble
\usepostamble\listoffiles
\from{\projectname.dtx}{luacode}%
}%
}%
Complete .ins file (the fragments above joined):
\catcode`\@=11%
\def\projectname{test}%
\input docstrip.tex
\keepsilent
\askforoverwritefalse
\begingroup
\catcode`\^^M=\active%
\obeyspaces%
\gdef\copyright{%
\ds@heading% This is file... generated with the docstrip utility.
\ReferenceLines% The original source files were: ...
This is a generated file.
Copyright 2019 by Phelype Oleinik
}%
\gdef\lppllicense{%
This work may be distributed and/or modified under the conditions of the
LaTeX Project Public License, either version 1.3c of this license or (at
your option) any later version. The latest version of this license is in
http://www.latex-project.org/lppl.txt
and version 1.3c or later is part of all distributions of LaTeX version
2005/12/01 or later.
}%
\gdef\listoffiles{%
This work consists of the files \projectname.dtx, and
\projectname.ins,
and the derived files \projectname.cls,
README.txt.%
}%
\endgroup
%
\generate
{%
\file{\projectname.cls}%
{%
\def\thispreamble{\copyright\lppllicense}%
\usepreamble\thispreamble
\usepostamble\listoffiles
\from{\projectname.dtx}{class}%
}%
}%
%
\generate
{%
\file{README.txt}%
{%
\def\MetaPrefix{}% Remove MetaPrefix for readme
\def\thispreamble{\copyright\lppllicense}%
\usepreamble\thispreamble
\usepostamble\listoffiles
\from{\projectname.dtx}{readme}%
}%
}%
%
\generate
{%
\file{luacode.lua}%
{%
\def\MetaPrefix{--}% Remove MetaPrefix for readme
\def\thispreamble{\copyright\lppllicense}%
\usepreamble\thispreamble
\usepostamble\listoffiles
\from{\projectname.dtx}{luacode}%
}%
}%
%
\endbatchfile
%% LINESand the read me normalLINES, solving my original problem. However, since my*.inscode is embedded within the*.dtxfile I did have some trouble running the*.dtxitself through PDFLaTeX which would complain that the===...===line did not appear after\begin{document}. Using the answer from a the related question I could stitch everything together and have included this as another answer. Thanks for the help !!! – Carel Jan 15 '19 at 18:45