tags:

views:

575

answers:

4

I screwed up my last post. Lets see if I can get this one right..

Would anyone be able to tell me how to pull the server name out of a UNC? (I'm looking for a regex)

this is the string I want to pull the servername from:

**\\\servername\d$\directory**

My Code is in C#

Regex r = new Regex(?????, RegexOptions.IgnoreCase);

Match m = r.Match(@"\\servername\d$\directory"); 

CaptureCollection cc = m.Captures;

foreach (Capture c in cc)
{

    System.Console.WriteLine(c);

}

I am looking to capture just: servername

no slashes or anything.

+4  A: 

This task is simple enough that it does not really require a regex (it would very much be overkill).

Try this approach, which uses a simple call to string.Split. It should allow you to get each part of the path trivially (though you say you don't need that).

var uncPath = @"\servername\d$\directory";    
var serverName = uncPath.Split('\')[1];

If you wanted to do the parsing fully manually, it's still quite straightforward:

var serverName = uncPath.Substring(1, uncPath.IndexOf('\', 1) - 1);
Noldorin
Thanks for the quick answer! This is likely the best way to do this..Yeah that's actually what I started with, but in this case I'm just looking for a simple example so I can understand regex better.
KevinDeus
FYI, C# split is a bugger with that escape character. I had to do this: string server = directory.Split('\\')[2];
KevinDeus
+10  A: 

Why use regex when Uri will do it too?

    Uri uri = new Uri(@"\\servername\d$\directory");
    Console.WriteLine(uri.Host); // servername
    Console.WriteLine(uri.IsUnc); // true
Marc Gravell
I like this one a lot. This one is really cool.
KevinDeus
A: 

Since you are trying to do it with regex, here is how i would do it. this produces the results you wanted.

string s = @"**\\\servername\d$\directory**";
Console.WriteLine(Regex.Match(s, @"[A-Za-z][A-Za-z0-9]*").Captures[0].Value);
Console.ReadLine();

produces servername

you might have to allow for some other potential servername characters like - and _ but this is the basic idea.

But using the Uri class is probably the best way to solve this specific problem.

Jason w
actually those asterisks were me trying to get that sentence to bold on this page. How would it change without them?
KevinDeus
it would be the same without them.
Jason w
it is just matching against the potential characters specified in the brackets in the [] brackets.
Jason w
+1  A: 

Note: This answer is for education purposes - better to use the Uri object and not re-invent existing functions.

Same solution as on the other question, but with backslashes reversed, and therefore escaped, (and possessive quantifier removed, since C# regex doesn't support that):

(?<=^\\\\)[^\\]+


The server name will be in \0 or $0 or simply the result of the function, depending on how you call it and what C# offers.


Explanation in regex comment mode:

(?x)      # flag to enable regex comments
(?<=      # begin positive lookbehind
^         # start of line
\\\\      # escaped backslashes to give literal \\ chars.
)         # end positive lookbehind
[^\\]+    # greedily match any non-\ until a \ or end of string found.
Peter Boughton