SgtOVERKILL
SgtOVERKILL

Reputation: 319

Regex for a shared path

I am having issues with a regex for server paths. I have many paths and they can differentiate like:

\\server1\folder\file

or

\\server2\folder\subfolder\file

I need to get the server name out of it and only the server name. I have tried using

[\\(.*?)\\]

which gets the inverse of what I want and multiple parts rather than just the server name. What is the proper expression?

Upvotes: 1

Views: 457

Answers (1)

adjan
adjan

Reputation: 13674

This regex matches everything between the \\ and the first \:

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

Explanation

  • (?<=\\\\) - starts with double \

  • [^\\]* - matches any character except \

Upvotes: 3

Related Questions