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?