.NET Framework Bookmark and Share   
 index > Regular Expressions > Regex to grab servername
 

Regex to grab servername

Hi,
I have a bit of a problem.
This is my string: \\dk-lm0400\ugs\ugs\
I want to grab ONLY dk-lm0400. The amount of slashes (folders) behind the server name (dk-lm0400) is variable.
I would have assumed this would take care of it:
\\\\(?<ServerName>\S+)\\
but it includes the first foldername aswell.
How do i make sure the ServerName group ends at the first \ it meets?
Nicolai Søndergaard LM Glasfiber A/S
Nicoolai
Hi,
I think this regex can help you to get the server name :
\\\\(?<ServerName>[^\\]+)

[^\\]+ : the ServerName group take all the part of the string without \ in it (so only the server name).
  • Marked As Answer byNicoolai Friday, September 18, 2009 8:24 AM
  •  
Sirdec
Hi,
I think this regex can help you to get the server name :
\\\\(?<ServerName>[^\\]+)

[^\\]+ : the ServerName group take all the part of the string without \ in it (so only the server name).
  • Marked As Answer byNicoolai Friday, September 18, 2009 8:24 AM
  •  
Sirdec
Thanks, that worked.
Nicolai Søndergaard LM Glasfiber A/S
Nicoolai

You can use google to search for other answers

Custom Search

More Threads

• Regular Expressions to Check for Mandatory Fields
• Extract only the IP Address which is read from TextFile using StreamReader
• Is there a way to have a named group stated ONCE and refer to it in the regex?
• Replace multiple spaces
• Getting a string from a Regex Pattern
• Detect a word in a line
• regular expression that matches pattern outside tags
• Mute system volume and microphone
• How enable Regular Expressions from within the VBA-code?
• How to find all occerences in a string