Regex fro commandline parameters

Category: c# general

Question

todomati on Fri, 05 Oct 2018 08:52:50


Hi Together

Could someone help me to Group this line (-l System -p 0 -r 10 -k "Log Files from Test" -z 35 -s server-dep-102) in Groups but pair based.

On example

-l System

-p 0

-r 10

-k Log Files from Test

and so on.....

All Parameters are comming from externall app and -k "Log Files from Test" comes without quotas

i have this regex pattern for it whcich goes only until first dash - in server-dep-102 and not to the and of the word.

I know that this happend because of this  [^-]* in pattern but i dont know how to avoid it and to match hole word which contain dash in it.

 regpattern = @"-[a-z]('[^']*'|( [^-]*)?)"

Exact Input string with no quotas after -k = "-l System -p 0 -r 10 -k Log Files from Test -z 35 -s server-dep-102"

Thanks in advance for your help


Replies

Viorel_ on Fri, 05 Oct 2018 12:41:01


Check this expression:

   -([a-z])\s*(.*?)(?=\s+(?:-|$))

 


todomati on Fri, 05 Oct 2018 12:47:03


Hi Viorel

Wow..

Thank you very very much...Great Job.

It took me long time cause still not familiar with grouping.

Its working perfect.....

ritehere44 on Sat, 06 Oct 2018 07:09:50


Hi Together

Could someone help me to Group this line (-l System -p 0 -r 10 -k "Log Files from Test" -z 35 -s server-dep-102) in Groups but pair based.

On example

-l System

-p 0

-r 10

-k Log Files from Test

and so on.....

All Parameters are comming from externall app and -k "Log Files from Test" comes without quotas

i have this regex pattern for it whcich goes only until first dash - in server-dep-102 and not to the and of the word.

I know that this happend because of this  [^-]* in pattern but i dont know how to avoid it and to match hole word which contain dash in it.

 regpattern = @"-[a-z]('[^']*'|( [^-]*)?)"

Exact Input string with no quotas after -k = "-l System -p 0 -r 10 -k Log Files from Test -z 35 -s server-dep-102"

Thanks in advance for your help


Your big problem is the bad design: you are using hifen ( - ) as flag for switches and at the same time you use it as part of the argument! This is a bad design decision.

The regex string you marked as the answer, fails to understand the last switch: -s server-dep-102

This happens exactly because of the bad design decision of using the hifen ( - ) as part of a argument.

The regex string can be made simpler if you use the hifen as a switch-flag only.