Search code examples
c#regexuriunc

Regular Expressions ~ convert UNC to URL


I'm looking for a nice tight regex solution to this problem. I'm looking to reformat an UNC into a Uri

Problem:

UNC directory needs to be reformatted into a Uri

\\server\d$\x\y\z\AAA

needs to look like:

http://server/z/AAA


Solution

  • I think a replace is easier to write and understand than Regex in this case. Given:

    string input = "\\\\server\\d$\\x\\y\\z\\AAA";
    

    You can do a double replace:

    string output = String.Format("http:{0}", input.Replace("\\d$\\x\\y", String.Empty).Replace("\\", "/"));