Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Removing duplicate substrings from a string - is a regex possible?

by wine (Scribe)
on Jul 23, 2001 at 18:26 UTC ( [id://99030]=note: print w/replies, xml ) Need Help??


in reply to Removing duplicate substrings from a string - is a regex possible?

OK, this is probably not the most beautiful and compact regex, but it works:

my $string = "New York/Chicago/New York/Chicago/Boston"; 1 while $string =~ s!(^|/)([^/]+)(.*)/\2(/|$)!$1$2$3$4!g; print $string; #outputs "New York/Chicago/Boston"

Update Well of course this is an abuse of regex. I would not use it myself nor recommend it, but it's a nice exercise.

  • Comment on Re: Removing duplicate substrings from a string - is a regex possible?
  • Download Code

Log In?
Username:
Password:

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

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

    No recent polls found