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.


In reply to Re^5: Regex find and replace involving new line by Corion
in thread Regex find and replace involving new line by PRA007

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.