in reply to Re: Regex find and replace involving new line
in thread Regex find and replace involving new line

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.

  • Comment on Re^2: Regex find and replace involving new line

Replies are listed 'Best First'.
Re^3: Regex find and replace involving new line
by Corion (Patriarch) on Dec 14, 2015 at 10:24 UTC

    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.

      My data files are exactly similar to what I have shown in the initial file. I have just installed strawberry perl so don't know how to code. I want to perform find and replace in text file using perl regular expression. I am fan of PCRE and have some knowledge. While asking for command line alternative to notepad++, I was suggested, perl is good choice. I tried for following file

      1

      2

      3

      4

      dog

      1

      2

      3

      4

      5

      puppy

      7

      8

      I want to replace dog with puppy. Sometimes I want preserve lines between them too sometimes and wan't to remove the lines other times.

      While using this one liner

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

      1

      2

      3

      4

      dog

      while notepad pcre gives desired result wile using find (dog)((.*[\r\n]+){6})(.*) and replace with \1

      1

      2

      3

      4

      7

      dog

      8

      like wise when I replace it with \2 it gave all the lines without dog and puppy. and while \3 gave me puppy in place of dog in above mentioned result. all behaves perfectly.

      So the main thing that I want is to make grouping as follow. group 1= dog, Group 2 = 6 lines after found string i.e.dog. group 3 = whatever present on line 7. I want to be able to put back the groups as required.

      Questions un answered

      How to use grouping in perl while using regex that matches new line? Am I using rightly grouping in perl in my above code?

      How to specify substitution? does substitution formula is correct as a whole in my code?

      How to specified remembered groups? Am I making any mistake in substitution position while specifying group like $1, $2, $3 or sometimes $& (what is found)

      I don't have sufficient knowledge to understand what you have suggested so I tried

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

      gave same results as with earlier oneliner

        When I run your regular expression

        (dog)((.*[\r\n]+){6})(.*)

        in Notepad++ with the following text:

        1 2 3 4 dog 1 2 3 4 5 puppy 7 8

        I also get

        1 2 3 4 dog 7 8

        Which is what I would expect, because the replacement only specifies \1, not \2 or \3.

        To your questions:

        How to use grouping in perl while using regex that matches new line? Am I using rightly grouping in perl in my above code?

        Captured groups are referenced in Perl using $1 for the first opening parenthesis, $2 for the second opening parenthesis and so on. You've used $1 in your existing Perl code, and from your description I think you would also want to use $2 and $3 maybe.

        How to specify substitution? does substitution formula is correct as a whole in my code?

        You use the s/// operator, as you already do.

        How to specified remembered groups? Am I making any mistake in substitution position while specifying group like $1, $2, $3 or sometimes $& (what is found)

        You use $1 and its siblings.

        Assuming that the output text you want (which you do not show us, even though I had recommended this) is the following:

        1 2 3 4 dog=puppy 7 8

        the following works for me:

        s/(dog)(([^\n]*[\n]){6})(.*)/$1=$4/g;

        If my interpretation of what you want is wrong, maybe now is a good time to be more specific and show concrete examples of what you want, and the exact cases when you want the specific results. "Sometimes this and sometimes that" is not a specific explanation.