Regular Expressions 101

Community Patterns

Extraneous slashes in pathnames

1

Regular Expression
PCRE (PHP <7.3)

~
(?P<leading> (?<=^/) /+ (?# Matches extra slashes after the first one) ) | (?P<middle> (?<!^) (?<=/) /+ (?!$) (?# Matches extra slashes in the middle of the path) ) | (?P<trailing> (?<!^) /+ (?=$) (?# Matches all trailing slashes) )
~
gmxX

Description

This regular expression matches all extraneous slashes in pathnames, that can be removed without altering which file the path name resolves into.

It is intended to be used like so:

# Replace $extraslashregex with the actual regular expression.
my $cleanpath = $path ~= s/$extraslashregex//gmxXr;
Submitted by anonymous - 5 years ago