tags:

views:

705

answers:

3

Hey,

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

A: 

Two operations:

  • first, replace "(.*)d\$\\x\\y\\(.*)" with "http:\1\2" - that'll clear out the d$\x\y\, and prepend the http:.

  • Then replace \\ with / to finish the job.

Job done!

Edit: I'm assuming that in C#, "\1" contains the first parenthesised match (it does in Perl). If it doesn't, then it should be clear what is meant above :)

Jeremy Smyth
+1  A: 
^(\\\\\w+)\\.*(\\\w\\\w+)$
  • First match: \\server

  • Second match: \z\AAA

Concatenate to a string and then prepend http: to get http:\\server\z\AAA. Replace \ with /.

Alan Haggai Alavi
+1  A: 

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("\\", "/"));
Jeff Meatball Yang
this one is awesome. Thanks :)
KevinDeus