PRA007 has asked for the wisdom of the Perl Monks concerning the following question:

I am using strawberry perl for windows.

I have following rtf text for example for performing regex find and replace

1

2

3

4

dog

1

2

3

4

5

puppy

7

8

I am using following regex in notepad++ for testing which works. I don't know how to use the same in perl. I have tried all the way but could not make it happen. below is one of my one liner.

 perl -i.bak -pe "BEGIN{undef $/;} s/(dog)(.*[\r\n]+){6}(.*)/$&$1=$3/smg" 4.txt

8

Here I want to identify dog = puppy. How to identify string that are fixed lines apart and do the replacements?

Replies are listed 'Best First'.
Re: Regex find and replace involving new line
by Corion (Patriarch) on Dec 14, 2015 at 09:57 UTC
    (.*[\r\n]+)

    I think you need to be far more specific to tell the RE engine to skip exactly six lines. I would specifically exclude newlines in the .* part and explicitly require a newline \n to delimit a line:

    ([^\n]*\r?\n){6}
Re: Regex find and replace involving new line
by choroba (Cardinal) on Dec 14, 2015 at 10:16 UTC
    How is your question different to the Stack Overflow one?
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      It is exactly the same. I initially thought that I have cracked the code but it is not workin. I have been blocked in stackoverflow for misguidance and can not edit the post.

      I have tried entire weekend but could not make this work.

        Maybe now is a good time to post the actual code you're using, together with a representative, short example of the data you're using. Also tell us exactly how your code does "not work". That means telling us what it outputs, and what you think it should output instead.

Re: Regex find and replace involving new line
by Anonymous Monk on Dec 14, 2015 at 12:42 UTC
    I am using following regex in notepad++ for testing which works. I don't know how to use the same in perl.
    I'm curious... how do you actually use single-line mode (/s) in notepad++? I bet you don't. Btw, /mg are also completely unnecessary there (but at least they don't do anything in your example).