8.21. Extract the Server and Share from a UNC Path
Problem
You have a string that holds a (syntactically) valid path
to a file or folder on a Windows PC or network. If the path is a UNC
path, then you want to extract the name of the network server and the
share on the server that the path points to. For example, you want to
extract server
and share
from
\\server\share\folder\file.ext
.
Solution
^\\\\([a-z0-9_.$●-]+)\\([a-z0-9_.$●-]+)
Regex options: Case insensitive |
Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby |
Discussion
Extracting the network server and share from a string known to hold a valid path is easy, even if you don’t know whether the path is a UNC path. The path could be a relative path or use a drive letter.
UNC paths begin with two backslashes. Two consecutive backslashes are not allowed in Windows paths, except to begin a UNC path. Thus, if a known valid path begins with two backslashes, we know that the server and share name must follow.
The anchor ‹^
›
matches at the start of the string (Recipe 2.5). The fact that the caret also matches at
embedded line breaks in Ruby doesn’t matter, because valid Windows paths
don’t include line breaks. ‹\\\\
› matches two literal backslashes. Since the
backslash is a metacharacter in regular expressions, we have to escape a
backslash with another backslash if we want to match it as a literal
character. The first character class, ‹[a-z0-9_.$●-]+
›, matches the name of the network server. The second one, after another literal ...
Get Regular Expressions Cookbook, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.