String regex = "^-admin -s ([^\\s]+) -port (\\d{4}) -u ([^\\s]+) -konf +([^\\s]+.xml)( -pause)?( -start)?( -stop)?( -save)?( -clean)?( -stat)?( -upload [^ ]+)?( -download [^ ]+)?$";
I need to catch the parameters when my program starts. The initial parameters are required but I have one optional parameter that if provided needs to be one from a specific list.
I use the groups I put the parameters in to check if the optional parameter exists, and if there is only one. If so, I use the name of that parameter to call the associated command. Basically I count the groups that are after the -konf
parameter (and xml filename) and if there is more than one parameter I write an error message.
Can I use only regex to make the last parameter optional, and only one from a list? For example, the user should be able to write any one of these parameters:
-pause
-start
-stop
-save
-clean
-stat
-upload datoteka
-download datoteka
Examples:
// pass
-admin -s localhost -port 8000 -u username -konf file.xml -pause
-admin -s localhost -port 8000 -u username -konf file.xml -save
-admin -s localhost -port 8000 -u username -konf file.xml -upload datoteka
These should pass, but the next examples should not because there is more than one command after the -konf
parameter.
// fail
-admin -s localhost -port 8000 -u username -konf file.xml -pause -save
-admin -s localhost -port 8000 -u username -konf file.xml -pause -clean