Tuesday 15 June 2010

regex - Strict regular expression for shared/network folder -


I assign a user's input to a valid shared folder path such as

\ \\ computer name \ Drive \ optional_folder1 \ optional_folder2 \

  1. starts with 2 slashes, followed by computer name, single slash and drive letter. (I got this part below)
  2. Ends with 0 or 1 slash.
  3. There may be zero or more than one single slash after the folder name (\ folder).
  4. No slash except the first one.

I have tried to look around and around and tried to make my own resume, but I could not find the right answer. This is my current regedx:

^ ((\\ {2}) ([A-Za-z -._] +) (\\ {1}) ([A-za -z -._] +)) (\\ {1} ([A-Za-z. -_]) +) * (\\?) $

any sign

I've simplified it a bit:

  ^ (\\) (\\ [A-Za-z0-9 -_] +) {2,} (\\?) $  

So basically you want

  ^ (\\) # Start with slash (\ [[A-Za-z0-9 -_] +) # after the group of slashes and name {2,} # Which should be two or more times (\\?) $ # Last Slash (SA)  

Demo:


No comments:

Post a Comment