Vikram Kamath

April 12, 2006

Regular Expression for folder paths

Filed under: Programming — Vikram @ 13:15

I have been looking for a regular expression that allowed me to validate a folder path on a local machine or a shared directory path using UNC with server name or UNC with IP Address. I did find a regular expression on But it didn’t seem to be solving my purpose. So I went about modifying it. I ended up creating different one. Many of you may have done this already. But this was a small exercise on my part to get to build a regular expression. So here it is.

Regular Expression: ^((([a-zA-Z]:)(\\{2}[a-zA-Z]+)(\\{2}\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))(?=(\\(\w[\w ]*)))(\\\w[\w ]*)*)$

Matches: c:\folder one\folder2\folder 3 , \\remoteMachine\folder1, \\\folder 1\folder2Non Matches: c:\\folder one c:folder 1, c:\folder 1\\folder 2, \remoteMachine\folder 1, \\remoteMachine\\folder 1, \\remoteMachine\folder 1\\folder2, \\\\folder 1, \\\folder 1\\folder2

Hope this helps anyone who needs to use it.

Blog at