in reply to Re: Using Look-ahead and Look-behind
in thread Using Look-ahead and Look-behind

You're reading your input line-by-line but you're trying to match across lines. That won't work.

The following should work, reading the input all at once (see perlrun):

perl -g -i -pe ’s/(?<=look)\n(?=ahead)/-/g;’ myfile.txt

Replies are listed 'Best First'.
Re^3: Using Look-ahead and Look-behind
by akiemr19 (Initiate) on Nov 28, 2024 at 13:24 UTC

    Thank you very, very much for setting me on the right path! While (in my environment) the -g switch isn't allowed for (what I learned to be was) slurping, instead using the -0777 switch did work nicely. I had seen this switch before, but until now wasn't aware of its usage until I dug deeper, prompted by your input.

    I was stuck on this for a long time - thanks again for the quick response!

    P.S. I just found where version 5.36 would allow the use of the -g switch, which is apparently a shortcut for using -0777.

      The -g only came in with perl 5.36 so won't be available in many LTS platforms unless they backport it. Using -0777 as you have mentioned is perfectly fine and, AFAIAA, will continue to be.


      🦛