As others have pointed out, if the ultimate goal is to remove lines starting with //# , for performance reasons you are probably better off using grep or sed :
grep -v '^\/\/#' filename.txt > filename.stripped.txt sed '/^\/\/#/d' filename.txt > filename.stripped.txt
or
sed -i '/^\/\/#/d' filename.txt
if you prefer in-place editing.
Note that in perl your regex will be
m{^//
which matches two slashes followed by # at the beginning of the line.
Note that you avoid "backslashitis" by using the m{pattern} matching operator instead of the more familiar /pattern/ . Immerse yourself in this syntax earlier, as this is an easy way to avoid excessive acceleration. You can write m{^//#} as effectively as m%^//#% or m#^//\## , depending on what you want to match. Strive for clarity - regular expressions are complex enough to decipher avoided backslashes without a prickly forest. Seriously, m/^\/\/#/ looks like a toothed alligator with a filling or tiny ASCII picture in the Alps.
One of the problems that can occur in your script is that the whole file is laid out in a line, new lines and thatβs it. To protect against this case, use the / m (multi-line) modifier in the regular expression:
m{^//
This allows ^ to match at the beginning of a line and after a new line. You might think that there is a way to split or match strings matching m{^//#.*$} Using the regex /g , /m and /s modifiers in case you cut the file into a string, I want make a copy of it (beg the question of why it was first put in a string). It should be possible, but it's late, and I do not see the answer. However, one βsimpleβ way to do this is:
my $cooked = join qq{\n}, (grep { ! m{^//} } (split m{\n}, $raw));
although this creates a copy instead of in-place editing in the $raw source line.