Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your regex is badly chosen on several points:</p> <ol> <li><p>Instead of matching two slashes specifically, you use <code>..</code> to match two characters that can be anything at all, presumably because you don’t know how to match slashes when you’re also using them as delimiters. (Actually, dots match <em>almost</em> anything, as we’ll see in #3.)</p> <p>Within a slash-delimited regex literal, <code>//</code>, you can match slashes simply by protecting them with backslashes, eg. <code>/\/\//</code>. The nicer variant, however, is to use the longer form of regex literal, <code>m//</code>, where you can choose the delimiter, eg. <code>m!!</code>. Since you use something other than slashes for delimitation, you can then write them without escaping them: <code>m!//!</code>. See <a href="http://p3rl.org/op#Quote-and-Quote-like-Operators" rel="noreferrer">perldoc perlop</a>.</p></li> <li><p>It’s not anchored to the start of the string so it will match anywhere. Use the <code>^</code> start-of-string assertion in front.</p></li> <li><p>You wrote <code>[^\n]</code> to match “any character except newline” when there is a much simpler way to write that, which is just the <code>.</code> wildcard. It does exactly that – match any character except newline.</p></li> <li><p>You are using parentheses to group a part of the match, but the group is neither quantified (you are not specifying that it can match any other number of times than exactly once) nor are you interested in keeping it. So the parentheses are superfluous.</p></li> </ol> <p>Altogether, that makes it <code>m!^//#.*!</code>. But putting an uncaptured <code>.*</code> (or anything with a <code>*</code> quantifier) at the end of a regex is meaningless, since it never changes whether a string will match or not: the <code>*</code> is happy to match nothing at all.</p> <p>So that leaves you with <code>m!^//#!</code>.</p> <p>As for removing the line from the file, as everyone else explained, read it in line by line and print all the lines you want to keep back to another file. If you are not doing this within a larger program, use perl’s command line switches to do it easily:</p> <pre><code>perl -ni.bak -e'print unless m!^//#!' somefile.txt </code></pre> <p>Here, the <code>-n</code> switch makes perl put a loop around the code you provide which will read all the files you pass on the command line in sequence. The <code>-i</code> switch (for “in-place”) says to collect the output from your script and overwrite the original contents of each file with it. The <code>.bak</code> parameter to the <code>-i</code> option tells perl to keep a backup of the original file in a file named after the original file name with <code>.bak</code> appended. For all of these bits, see <a href="http://p3rl.org/run" rel="noreferrer">perldoc perlrun</a>.</p> <p>If you want to do this within the context of a larger program, the easiest way to do it safely is to open the file twice, once for reading, and separately, with <a href="http://p3rl.org/IO::AtomicFile" rel="noreferrer">IO::AtomicFile</a>, another time for writing. IO::AtomicFile will replace the original file only if it’s successfully closed.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload