in reply to Re^4: Regex find and replace involving new line
in thread Regex find and replace involving new line
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Regex find and replace involving new line
by PRA007 (Initiate) on Dec 15, 2015 at 08:44 UTC | |
by AnomalousMonk (Archbishop) on Dec 15, 2015 at 13:17 UTC | |
by Corion (Patriarch) on Dec 15, 2015 at 09:21 UTC | |
by PRA007 (Initiate) on Dec 15, 2015 at 12:13 UTC | |
|
Re^6: Regex find and replace involving new line
by PRA007 (Initiate) on Dec 15, 2015 at 07:19 UTC | |
|
Re^6: Regex find and replace involving new line
by PRA007 (Initiate) on Dec 15, 2015 at 06:16 UTC |