Willy
Willy

Reputation: 10648

Regular Expressions: replace extra slashes/backslashes from a path

I have a string which I would like to remove extra slashes following below criteria:

So I have tried below without success:

string myPath = "/////this///is//an////absolute//path";
        

// below check if there are more than 4 slashes at the beginning. If so replace them by 4 slashes
string url  = System.Text.RegularExpressions.Regex.Replace(myPath  , @"^(/+){4}", @"////");
// below check if there are extra slashes (more than 2) in other parts of string (excluding the beginning of the string). If so, replace them by 2 slashes.
url= System.Text.RegularExpressions.Regex.Replace(url, @"(/+){3}", @"//");

Example:

string = "/////this///is//an////absolute//path"

Expected url:

////this//is//and//absolute//path

Current url:

//this//is//and//absolute//path

Also I would like to do it in a single regex instead of split it in two statements and working in case of backslashes instead of slashes, for example, the same regular expression should be able to do it as well:

\\\\\this\\\is\\an\\\\absolute\\path

Expected:

\\\\this\\is\\an\\absolute\\path

Also It should be valid for below paths (with all slashes or backslashes):

c:\\\\\this\\\is\\an\\\\absolute\\path

Expected:

c:\\this\\is\\an\\absolute\\path

So there are cases where string comes with all slashes or all backslashes and I need it to work for both cases.

Upvotes: 1

Views: 981

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626870

I suggest

Regex.Replace(text, @"^(([/\\])\2{3})\2+|([/\\])\3*", "$1$3$3")

See the regex demo. Details:

  • ^ - start of string
  • (([/\\])\2{3}) - Group 1 capturing a / or \ captured into Group 2 and then matching the char captured into Group 2 three times
  • \2+ - one or more occurrences of Group 2 value
  • | - or
  • ([/\\])\3* - Group 3 capturing a / or \ and then matching zero or more (back)slashes.

Upvotes: 2

Related Questions