...

View Full Version : More RegEx validating stuff



WolfShade
07-19-2012, 11:15 PM
Hello, everyone.

I'm going to be heading home, shortly, so won't respond until tomorrow morning; but I could use some help with a RegEx test mask.

Basically, I'm trying to validate for both Windows and *nix paths.

Good is:
[drive letter]:\folder1\folder2\folder_3\folder-4
\\server\share\folder_1\folder-2
//usr/folder_1/folder-2

Bad could be
[anything other than drive letter or "\\"]folder\\folder_1\/
\\\server\\folder\\folder_2
///?%$\folder/folder_2//bad_path

Here is what I have, so far. I'm getting most of the validation I want from it, but I just can't seem to get past the last few details.


/^(([a-z]\:\\|\\{2})[\w\d\\\.\_\-]+|\/[^\/{2,}][\w\d\/\.\_\-]+)$/
red = windows, blue = *nix


Thank you,

Old Pedant
07-20-2012, 12:14 AM
\w\d ??

\w is the same as [a-zA-Z0-9_]
\d is the same as [0-9]

Redundant.

[\w\\\.\-]+

Would be sufficnent.

But many other characters are legal in folder names, certainly in windows. Space, %, $, and several others.

Here:

Use any character in the current code page for a name, including Unicode characters and characters in the extended character set (128–255), except for the following:

The following reserved characters:
< (less than)
> (greater than)
: (colon)
" (double quote)
/ (forward slash)
\ (backslash)
| (vertical bar or pipe)
? (question mark)
* (asterisk)
Integer value zero, sometimes referred to as the ASCII NUL character.
Characters whose integer representations are in the range from 1 through 31 [with some exceptionsl]

Old Pedant
07-20-2012, 12:18 AM
And this makes no sense:

[^\/{2,}]

When you use [^...] that means "allow ANY character except those listed". And by and large, you don't need to excape characters after the ^.

So your expression is saying "any characters except / and { and 2 and , and }"

Which surely isn't what you meant.

WolfShade
07-20-2012, 04:19 PM
I use RegEx so rarely, every time I start to learn it I get done what needs doing and then there's no need for it and I focus on other things. So despite my years of developing, I am still not completely familiar with all the rules and such.

I'm tweaking and tweaking, playing with it. Once I get what I need, I'll post the solution.

Thanks,



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum