in reply to Regex: remove non-adjacent duplicate hashtags
#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11145665 use warnings; $_ = "#tag1 #tag2 #tag3 #tag1\n"; print " original: $_"; s/(#\w{2,})\b\h*(?=.*\1\b)//g; print " keeping only the last one: $_"; $_ = "#tag1 #tag2 #tag3 #tag1\n"; 1 while s/(#\w{2,})\b.*\K\1\b\h*//; print "keeping only the first one: $_";
Outputs:
original: #tag1 #tag2 #tag3 #tag1 keeping only the last one: #tag2 #tag3 #tag1 keeping only the first one: #tag1 #tag2 #tag3
|
|---|