Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Regex: remove non-adjacent duplicate hashtags

by LanX (Saint)
on Jul 23, 2022 at 13:36 UTC ( [id://11145668]=note: print w/replies, xml ) Need Help??


in reply to Regex: remove non-adjacent duplicate hashtags

> 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)

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11145668]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-25 05:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found