UNC and folder path regular expressions

2,620 views
Skip to first unread message

am...@c4ppy.net

unread,
Apr 22, 2005, 7:04:46 AM4/22/05
to re...@googlegroups.com
Hi.

I need two regularexpressions that will work in .Net

The first one is for valid unc paths to file shares..

server,share,folder names can be any vailid windows name

so these are valid
\\server\share
\\server\share name
\\server\share$
\\server\share$\folder

etc

these are not valid
\\server\share\ <- cant end in slash
\\server\share\file.ext


The second one is similar but for folders
valid
c:\
c:\foldername
c:\folder name
c:\foldername\foldername

no valid
c:
c:\foldername\
c:\foldername\foldername\
c:\foldername\file.ext

so again can not end in a slash unless its a root path. All valid
windows folder names allowed.

Any help appreciated as regular expressions.. I just cant get my head
round.

Thanks
Amit

Eugeny Sattler

unread,
Apr 22, 2005, 7:34:49 AM4/22/05
to re...@googlegroups.com
Hi Amit,

> so these are valid
> \\server\share
> \\server\share name

If above line is not bound by sharp brackets like <\\server\share name>
how should this made-from-iron-computer guess where unc path ends?
Imagine it is inside a letter like "I welcome you to vist
\\server\share name this is really a cool software repository!"
Should computer understand it as <\\server\share name this> or as
<\\server\share name this is>
Even a human can not guess.

This is why the mankind invented sharp brackets. :))

What will play a role of closing sharp bracket in your case? pls
clarify the task.

Eugeny Sattler

unread,
Apr 22, 2005, 11:07:08 AM4/22/05
to re...@googlegroups.com
> these are not valid
> \\server\share\file.ext

as far as I know folder names can contain dots. Try it yourself -
create a folder in your file system with dot in the middle. It will be
accepted and will work as a normal folder. Wouldn't you like to soften
the requirements?

Eugeny Sattler

unread,
Apr 22, 2005, 12:46:04 PM4/22/05
to re...@googlegroups.com
also why "C:" is not valid but "C:\" is valid?
my windows explorer recognizes both variants.

I am trying to make a iniversal regexp both for UNC path and for windows paths.
Although you asked for two separate regexes. :))

So far I have created this
^([a-z]:|\\\\[a-z]+\\(?!\.)[^\r\n$<>]+\$?)(\\|(\\(?!\.)[^\r\n<>\\]+)*)(?<!\\)$

the only problem with this one, it does not match "C:\"
but matches all other things I think are valid (listed below):

\\server\share
\\server\share name
\\server\share$
\\server\share$\folder
\\server\share$\fol.der.name
c:\foldername
c:\folder name
c:\foldername\foldername
c:
c:\foldername\file.ext

Pls anybody feel free to suggest anything better. Also a weak side of
my regex is that it does not allow dollar signs in the middle of share
name. Technically they can be there... But if I take out dollar sign
from the mid character class the regex starts allowing folder names
starting from dot.
--
Eugeny

am...@c4ppy.net

unread,
Apr 25, 2005, 2:56:52 AM4/25/05
to re...@googlegroups.com
Hi..

Thanks for the help.

The reason I am using the reg ex is to validate input into a fileserver
database. Two of the input fields are share name, and physical path.

Thats why I need seperate expressions for each field.

How can I split the expression you have created into two?

With regards to where to end the expression.. can you not read it up to
the end of line? The data is coming from a user input textbox so
everything entered needs to be valid.

"\\server\share name this is really a cool software repository!" is a
valid share name

also.. with regards to the slash.. after C:\ can we make it so we can
have an optional slash at the end? so c: and c:\ are vaild? Same with
the foldernames.. if c:\foldername\ is valid, I can deal with the
trailing slash in code.

So basicaly.. if you could modify the regex to allow \ at the end of a
folder name, and also split the UNC and the Physical path names that
would be great.

Also.. where is a good place to learn regular expressions.. ?

Amit

Eugeny Sattler

unread,
Apr 26, 2005, 7:47:48 AM4/26/05
to re...@googlegroups.com
> Also.. where is a good place to learn regular expressions.. ?
Browse back archives of this group. not very long ago I posted links to books

You may also want to check
www.regular-expressions.info
www.regex.info
http://aspn.activestate.com/ASPN/Reference/Products/ActivePerl/lib/Pod/perlre.html
http://aspn.activestate.com//ASPN/Reference/Products/ActivePerl/lib/Pod/perlop.html
www.regexpstudio.net


--
best regards, Eugeny

Eugeny Sattler

unread,
Apr 29, 2005, 10:43:05 AM4/29/05
to re...@googlegroups.com
> also.. with regards to the slash.. after C:\ can we make it so we can
> have an optional slash at the end? so c: and c:\ are vaild? Same with
> the foldernames.. if c:\foldername\ is valid, I can deal with the
> trailing slash in code.

> So basicaly.. if you could modify the regex to allow \ at the end of a
> folder name, and also split the UNC and the Physical path names that
> would be great.

pls test this on your data
^((?:[a-z]:\\$|(?:[a-z]:|\\\\[a-z]+\\(?!\.)[^\r\n$<>]+\$?)))((\\|(\\(?!\.)[^\r\n<>\\]+)*)(?<!\\)$)

then explore contents of $1 and $2 variables
$1 variable shoud contain either "C:" or "\\server\share$"
while $2 variable will contain the remaining path
like "\folder\subfolder\subsubfolder\file.ext"

^ plays the role of start of input field point.
$ plays the role of end of input field point.
So don't worry that I am reading data up to the end of line.
Reply all
Reply to author
Forward
0 new messages