0

I am programming a tool to standardize batch-files and I use a regular expression to validate every net use command in it.

I'm stuck at the directory that is given to the command. The standard syntax of the command in this case is: net use [devicename]: [path] [/user:"\domain\user"] [password] [/persistent:no] where the [path] can contain whitespaces. If true it has to be between quotationmarks("), otherwise there have to be no quotationmarks. The user and password are optional. The path can contain the following special characters (!,,,+,.,-).


The following regular expression allows a path in quotationmarks without whitespaces and disallows a path with sequenced whitespaces with quotationmarks or without whitespaces and quotationmarks. The regex may contain syntaxerrors, I tested it on regex101.com with PCRE:

Regex pattern = new Regex(@"^net use [a-z]:\s
(?(?=[\S]+\s[\S]+)(""\\\\[\w.\-,!+]+(\s[\w.\-,!+]+)*(\\[\w.\-,!+]+(\s[\w.\-,!+]+)*)*"")|
(\\\\[\w.\-,!+]+(\\[\w.\-,!+]+)*))
(\s\/user:""[a-zA-Z0-9]+(\\[a-zA-Z0-9]+)*"")?\s\/persistent:no$");

The necessary part of the regex is:

(?(?=[\S]+\s[\S]+)(""\\\\[\w.\-,!+]+(\s[\w.\-,!+]+)*(\\[\w.\-,!+]+(\s[\w.\-,!+]+)*)*"")|
(\\\\[\w.\-,!+]+(\\[\w.\-,!+]+)*))

How do I afford the expression to do it vice versa?

Dipen Shah
  • 1,911
  • 10
  • 29
  • 45

1 Answers1

1

I can help you with the directory matching.

Let's start with the way that directories can be prefixed.

./ e.g. ./some/path/to/some/file.txt

../ e.g. ../some/path/to/some/file.txt

/ e.g. /some/path/to/some/file.txt

<blank> e.g. some/path/to/some/file.txt

We can describe this with this regex.

(\.\/|\.\.\/|\/)? Definitely unsightly, but it works.

Here's an expanded diagram of our regex before it gets condensed into a one-liner.

"                   # opening quotation mark
(\.\/|\.\.\/|\/)?   # optional / ./ ../
[^\/"]++            # match content after opening / ./ ../
(\/[^\/"]++)+       # match /<CONTENT> repetitively
"                   # closing quotation mark

Let's wrap the expression in spaces (maybe more than one) to be safe. [ ]+

[ ]+"(\.\/|\.\.\/|\/)?[^\/"]++(\/[^\/"]++)+"[ ]+

This should match paths with spaces as long as they're wrapped in quotes. Here are some trials that I ran with it.

As for paths without spaces that aren't enclosed in quotations try this regex.

[ ]+(\.\/|\.\.\/|\/)?[^\/\s]++(\/[^\/\s]++)+[ ]+

It shares features with the other regex, so hopefully you can use that as a reference point to understanding this one. I'm not sure how much this helps your problem overall, but I guess it should shed a little light onto paths portions.

Oh, and to use them in conjunction, just your the regex or operator, |. Should look something like this:

(                                                # begin capturing group
[ ]+"(\.\/|\.\.\/|\/)?[^\/"]++(\/[^\/"]++)+"[ ]+ # paths with spaces
|                                                # or
[ ]+(\.\/|\.\.\/|\/)?[^\/\s]++(\/[^\/\s]++)+[ ]+ # paths w/o spaces
)                                                # end capturing group

Good luck!

wpcarro
  • 1,528
  • 10
  • 13
  • The expression with spaces accepts paths wrapped in quotationmarks without spaces ([link](https://regex101.com/r/fM2rF5/1)). Is there a way to disallow this? For example with a conditional expression that the path must have one space at least. – RNGsus Cryst Jun 30 '16 at 08:20
  • @RNGsusCryst is it problematic that paths w/o spaces would have quotation marks? Isn't that valid formatting nonetheless? – wpcarro Jun 30 '16 at 14:31
  • @RNGsusCryst `(?<=\s)(?<!")(?:\.\/|\.\.\/|\/)[^\s]+(?!")(?=\s)` this will match only paths that you described in your comment. Try it out in the link you provided previously. Not sure how you would combine all of these though. – wpcarro Jun 30 '16 at 14:38
  • @wcaroll No, it is not problematic, the code is valid nonetheless. I was interested, if there is a solution for this problem. I will wrap all `[paths]` in **quotationmarks** as you suggested. That will provide a better perfomance for the program than my regex. Many thanks for your help. – RNGsus Cryst Jun 30 '16 at 20:47