Community Patterns

Community Library Entry

1

Regular Expression
Created·2024-02-29 08:14
Updated·2024-02-29 08:17
Flavor·PCRE2 (PHP)

~
([^:])(/{2,})
~
g
Open regex in editor

Description

([^:]): This is a capturing group that matches any character except a colon :, and it captures this character for later use in the replacement. (/ {2,}): This part matches two or more consecutive forward slashes. The replacement part $1/ uses the captured character from the first part (which is any character except a colon) followed by a single forward slash. This effectively replaces multiple consecutive forward slashes with a single slash, excluding cases where the slashes appear after a colon.

Here's a breakdown of how it works:

([^:]): Match any character that is not a colon and capture it. (/ {2,}): Match two or more consecutive forward slashes. $1/: Replace the matched pattern with the captured character followed by a single forward slash. This regular expression is useful for cleaning up URLs by removing redundant consecutive forward slashes, except those immediately following a colon.

Submitted by Apon Ahmed