Home > Enterprise >  Regular Expressions: replace extra slashes/backslashes from a path
Regular Expressions: replace extra slashes/backslashes from a path

Time:02-15

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

  • If extra slashes are at the beginning of the string I want to remove them if there are more than 4 in a row. I want to replace them by 4 slashes.
  • If extra slashes are not at the beginning but in other places of the string, I want to remove them and replace them by 2 slashes if there are more than 2 in a row.

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.

CodePudding user response:

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.
  • Related