Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Regex: remove non-adjacent duplicate hashtags

by tybalt89 (Monsignor)
on Jul 23, 2022 at 18:54 UTC ( [id://11145669]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-03-28 21:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found