2

Trying to figure out a regex for validating a network path ie: \\comp\xyz or \\comp or \\comp\x\y\z\storage or something much more lengthy on all parts but the jist of it is hopefully conveyed.

What I have currently is a simple input field, that I have a user passing information through. Thing of it is I don't want them to put it in wrong as the backend connects to a client that uses it to run processes on computers across a network, so last thing I need is someone mistyping something and being the cause of something else breaking. So I figure a quick preg_match will do me just fine in confirming this from the PHP side I am currently work with but I do have a need to work this into JavaScript and a layer thats built on Ruby so I not knowing if all regex is equal or not I need one that works on all levels..

Also to save me time later in coming back I will eventually need to figure out a regex that will allow a user to do either local path ie: C:\, X:, H:\path\to\folder or a network path as mentioned prior.

4
  • I'm certain that PHP and Javascript use PCRE. I'd be surprised if Ruby doesn't as well. Commented Jun 15, 2012 at 20:43
  • As a tip I would recommend you free tool for regular expresions: Espresso Commented Jun 15, 2012 at 20:45
  • I think PCRE supports lookbehind, and JavaScript does not (???) Commented Jun 15, 2012 at 20:47
  • @Cfreak, What do you mean by "using PCRE"? PHP farms out its regex handling to the PCRE library, but JavaScript doesn't. If you just mean a flavor that's derived from Perl's, call it just that: Perl-derived. "PCRE" is hopelessly ambiguous. As for Ruby, yes it's another Perl-derived flavor, but its actual regex support is provided by the Oniguruma library. Commented Jun 15, 2012 at 21:31

5 Answers 5

4

\\comp\x\y\z\storage:

/^\\(\\[^\s\\]+)+(\\)?$/

H:\path\to\folder:

/^([A-Za-z]:(\\)?|[A-Za-z]:(\\[^\s\\]+)+)(\\)?$/

Both:

/^(\\(\\[^\s\\]+)+|([A-Za-z]:(\\)?|[A-z]:(\\[^\s\\]+)+))(\\)?$/

Working demo: http://jsfiddle.net/DerekL/gLfdv/

Sign up to request clarification or add additional context in comments.

12 Comments

matches \\\\\\\\\\comp\x\y\z\\\\\\\\storage - was this intended? (is this valid path? - I only know regex pretty well, not network paths as well, I guess)
only one character between each slash? (for the network path)
I was about to say it didn't match X:, but then you fixed that -- of course, unless I miss my guess, it now doesn't match `C:` - oh wait - there you go (don't forget the "both" expression, though)
It matches C: - /^([A-z]:(\\)?|[A-z]:(\\[^\s\\]+)+)$/.test("C:") returns true (Javascript)
there you go! looks good enough to me (I still think mine's simpler - but the better one is for the voters and OP to decide)
|
1

this seems to work for me:

([A-Z]:|\\)(\\[a-z0-9]+)*

let me know if you've got any problems (undesirable or additional desired) matches

and to validate a WHOLE field, use:

^([A-Z]:|\\)(\\[a-z0-9]+)*$

EDIT a small revision was necessary to match ALL of your suggestions (my bad):

^([A-Z]:|\\)(\\$|\\[a-z0-9]+)*$

7 Comments

[a-z0-9] So I can't use characters other than a to z and 0 to 1? ñç?
not unless you add them to the character class, nope - I was thinking the more important part of it was making sure the `\`'s were right, and additional characters would be easy enough to add.
not a clue - also depends upon WHAT you want to be able to match (for instance, ^([A-Z]:|\\)(\\$|\\\w+)*$ would probably be better than any in the answer at this point, and would probably work for any "English speaking" server - not sure how representative that is in a universal sense, but the question was asked in English and all the examples were in English... so...
:P naïve is an English word with the 2 dots above the i.
for SOME people, yes... but most will simply spell it with a normal i, but the real question is: is it something that many people would use in a network path or file system? Again - for the voters, OP, or anyone trying to implement this in their environment or application to determine for themselves.
|
1

For the first one I have:

^\\(\\[a-zA-Z_]\w*)+$

and the second one:

^[a-zA-Z]:(\\([a-zA-Z_]\w*)*)*$

Comments

0

General case:

/^(\\\\\w+)(\\\w+)$/g   // matches \\comp, \\comp\xyz, \\comp\x\y\z\storage

Specific case:

/^([A-Z]:)(\\|\\\w+)*$/g  // matches C:\, X:, H:\path\to\folder  

1 Comment

Maybe you're enclosing the regexp within quotes.
0

This will match UNC paths that start with an ip address and can handle spaces in the file / folder names. Also validates standard path formats e.g. C:\Users\User\some person\file.txt

/^((\\\\[a-zA-Z0-9-\.]+\\[a-zA-Z0-9`~!@#$%^&(){}'._-]+([ ]+[a-zA-Z0-9`~!@#$%^&(){}'._-]+)*)|([a-zA-Z]:))(\\[^ \\/:*?""<>|]+([ ]+[^ \\/:*?""<>|]+)*)*\\?$/

I modified the answers from: PHP Regex for matching a UNC path

and: http://regexlib.com/REDetails.aspx?regexp_id=2285&AspxAutoDetectCookieSupport=1

See it in action at https://jsfiddle.net/megamania/q5ncy7t6/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.