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