> What am I doing wrong?
you have multiple issues
- use \1 instead of $1 on the match side
- /g moves your start pos where the last match stopped, so you either need to use a lookahead (?=) or \K to keep the position after the first group
- your $1 will include whitespaces in the end and won't match at \n
here a demo with perl -dE0
DB<70> $r = " #1 #2 #3 #1 #3 #2 #3 #2"
DB<72> $_=$r; s/(#\S+\s?)(.+$1)(?{say "$_ ($1) - ($2) pos:",pos($_)})
+/$2/g ;say "fin: $_"
#1 #2 #3 #1 #3 #2 #3 #2 (#1 ) - (#2 #3 #1 #3 #2 #3 #2) pos:24
fin: #2 #3 #1 #3 #2 #3 #2
you can use this as template to experiment and debug with (?{...})
update
like using \K
DB<73> $_=$r; s/(#\S+\s?)\K(.+\1)(?{say "$_ ($1) - ($2) pos:",pos($_
+)})//g ;say
#1 #2 #3 #1 #3 #2 #3 #2 (#1 ) - (#2 #3 #1 ) pos:13
+
#1 #2 #3 #1 #3 #2 #3 #2 (#3 ) - (#2 #3 ) pos:22
+
#1 #3 #2
update
like using (?=...)
DB<81> sub DEB {say "$_ ($1) - ($2) pos:",pos($_)}
DB<82> $_=$r; s/(#\S+\s?)(?=(.+\1))(?{DEB})//g ;say "fin: $_"
+
#1 #2 #3 #1 #3 #2 #3 #2 (#1 ) - (#2 #3 #1 ) pos:4
+
#1 #2 #3 #1 #3 #2 #3 #2 (#2 ) - (#3 #1 #3 #2 ) pos:7
+
#1 #2 #3 #1 #3 #2 #3 #2 (#3 ) - (#1 #3 #2 #3 ) pos:10
+
#1 #2 #3 #1 #3 #2 #3 #2 (#3 ) - (#2 #3 ) pos:16
+
#1 #2 #3 #1 #3 #2 #3 #2 (#2) - ( #3 #2) pos:18
+
fin: #1 #3 #2
+
+
+
DB<83>
update
as you can see in the debug was \K not the best idea, but YMMV
update
you still need to fix the whitespace issue ...
... and IIRC there was a meta to keep the pos (\g or \G ?), please check perlre (I might misremember tho)