如何从UNC中提取服务器名称

Kev*_*eus 11 c# regex uri

有人能告诉我如何从UNC中提取服务器名称吗?

恩.

//服务器/目录/目录

编辑:我道歉但看起来我需要澄清一个错误:路径实际上更像是:

//服务器/ d $ /目录

我知道这可能会改变一些事情

Mar*_*ell 20

怎么样Uri:

Uri uri = new Uri(@"\\servername\d$\directory");
string[] segs = uri.Segments;
string s = "http://" + uri.Host + "/" + 
    string.Join("/", segs, 2, segs.Length - 2) + "/";
Run Code Online (Sandbox Code Playgroud)


Pet*_*ton 6

只是另一种选择,为了显示不同的选项:

(?<=^//)[^/]++
Run Code Online (Sandbox Code Playgroud)


服务器名称将在\0$0仅仅是函数的结果,具体取决于您调用它的方式以及您的语言提供的内容.


正则表达式注释模式中的说明:

(?x)      # flag to enable regex comments
(?<=      # begin positive lookbehind
^         # start of line
//        # literal forwardslashes (may need escaping as \/\/ in some languages)
)         # end positive lookbehind
[^/]++    # match any non-/ and keep matching possessively until a / or end of string found.
          # not sure .NET supports the possessive quantifier (++) - a greedy (+) is good enough here.
Run Code Online (Sandbox Code Playgroud)

  • @Kevin:只需删除第二个+符号. (2认同)