Regular Expressions 101

Community Patterns

Community Library Entry

1

Regular Expression
Created·2018-11-09 16:46
Flavor·PCRE (Legacy)

~
(?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
Open regex in editor

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