in reply to Regex: remove non-adjacent duplicate hashtags

You need to use \1 instead of $1, see perlre.

You may also want to move the \s? out of the capture group.

Update: Plus, what LanX said about using a lookahead!

Replies are listed 'Best First'.
Re^2: Regex: remove non-adjacent duplicate hashtags
by element22 (Novice) on Jul 23, 2022 at 13:24 UTC
    Thanks, it works. Here's my more elaborated version:

    $tags=~s/(#\S{2,}\b)(.*\s?)(\1)(\s?.*)/$3$2$4/g;