Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When you open a file for reading, the file is by default read line by line. This is because the input record separator <code>$/</code> is set to newline. </p> <p>Since you are breaking each "line" on newline, it stands to reason that you can never find two newlines in a row in a single line.</p> <p>One way to get around this is, as Zaid has said, to change the input record separator with the <code>-0</code> flag. As long as the new input record separator is not partially matched by your regex, you will be fine. (As long as you do not try to match <code>.</code> to <code>\n</code>).</p> <p>Let's make your regex a bit less horrid. You don't need to escape <code>#</code>, unless you are using the <code>/x</code> modifier. You don't need to use several <code>#</code> characters in a row, use a quantifier, <code>+, * or {x,y}</code>. </p> <p>Instead of removing a string and then putting back an identical one, there are options to avoid typing the same thing twice. </p> <ul> <li>You can avoid removing the string by using <a href="http://perldoc.perl.org/perlre.html#Look-Around-Assertions" rel="nofollow">Lookaround Assertions</a>.</li> <li>You can use <code>\K</code> as a simplified Look-behind assertion (see above)</li> <li>You can capture strings and put them back using <code>$1, $2 ...</code>.</li> </ul> <p>My preference in this case would be to use a look-behind assertion to find the "color\n\n" string, then a look-ahead to find the "Color" comment.</p> <pre><code>perl -0777 -pwe 's/(?&lt;=color\n\n)(?=[#\s]+Color)/INSERT\n\n/' /path/to/file </code></pre> <p>Where <code>INSERT</code> is of course your text to insert, which I removed for readability. I also removed the <code>-i</code> flag so you can try it out first.</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.
 

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