Regular Expression for folder paths
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 RegExLib.com. 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, \\1.22.33.444\folder 1\folder2
Non Matches: c:\\folder one c:folder 1, c:\folder 1\\folder 2, \remoteMachine\folder 1, \\remoteMachine\\folder 1, \\remoteMachine\folder 1\\folder2, \\1.22.33.444\\folder 1, \\1.22.33.444\folder 1\\folder2
Hope this helps anyone who needs to use it.





4 comments:
Thanks for your post. Its a great help for me.
Doesn't work buddy...
IT does not work !!
Code here is missing it's "OR" meta characters.
^((([a-zA-Z]:)|(\\{2}[a-zA-Z]+)|(\\{2}\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))(?=(\\(\w[\w ]*)))(\\\w[\w ]*)*)$
Post a Comment