Thursday, April 19, 2018

Match Network Path In A String Using Regular Expression


While working on a project, I was confronted with an issue on how to match or extract a network path. Normally, you can use string functions or built-in .NET code to check the validity of a path, but since I'm also refreshing my skills in Regular Expression. I prefer the latter as the solution. So the pattern to match the server path is presented below:
((\\\\)?((?<Folder>[a-zA-Z0-9- _]+)(\\.*[a-zA-Z0-9-_ \\])(?=")))
And to declare that pattern in your C# code, you have to escape the back slash characters.
string ServerPath = "((\\\\\\\\)?((?<Folder>[a-zA-Z0-9- _]+)(\\\\.*[a-zA-Z0-9-_ \\\\])(?=\")))";
The input strings tested are as follows.
Const ImagesPath As String = "\\ImagesServer\Logo Files\InetPub\TempProjects\"
Const docsPath As String = "\\docsPathServer15\Health\Physicians\"
Const vsPath As String = "\\vsServer909\vs2017\Projects"
string manual = "\\manualServer\CompanyRules\EmployeeFiles\Cebu"; 
The referenced post is here: Pattern Matching Path Files but I made some modifications to get the path inside the enclosing quotes.



Post a Comment