Search code examples
c#regexwhitespacequotation-marksnet-use

C# Regex: Check if the given directory in a net use command contains whitespaces


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?


Solution

  • 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!