Right argument, japhy!! But it's an interesting problem nevertheless. Your regex breaks on: New York/York/Boston => New York/Boston My fix makes it sadly less elegant:
Now this is close to my approach: 1 while s#(/|^)((?>[^/]*))/(?=(?:.*?/)?\2(?:/|$))#$1#g; The advantage of both solutions is that as little as possible is replaced. The disadvantage is that a regex isn't the right way to do it ;) - for all the reasons already given in this thread.1 while s{ (/|^) # add: capture slash or start of string: $1 ([^/]+) # non-slashes (the city): \2 / # a / (?= # look ahead for:... (?: # group (not capture) [^/]* / # city and / )*? # 0 or more times, non-greedily \2 # is the city here again? (?: /|$ ) # a / or end-of-string ) } {$1}gx; # add: replacement
-- Hofmator
In reply to Re4: Removing duplicate substrings from a string - is a regex possible?
by Hofmator
in thread Removing duplicate substrings from a string - is a regex possible?
by iakobski
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |