I would probably do the checking for whether a certain command should have a parameter or not in general purpose code (i.e. Java) rather than in the regular expression. That is to say, Regular Expressions are good for tokenizing, but not so much for parsing.
However, if you've got a good reason to do it that way, then I would probably split the expression into two parts - one where the commands require a command, and one where they do not. For example:
^\\(?:(?<command>login)\s+(?<param>\w+)|(?<command>status|logout)(?<param>))$
Note that the final (?<param>)
is not strictly necessary for the regular expression to operate correctly, it is merely there so that any subsequent code can rely on two named groups in the result: command and param.
Logically, this could be extended to three groups where the third group contains commands with an optional parameter.
This can more clearly be written like so if you're using Groovy (or Java 7) for multi-line strings:
^\\(?x:
(?<command>login) \s+ (?<param>\w+) # Commands that require a parameter
| # -or-
(?<command>status|logout) (?<param>) # Commands that do not require a parameter
)$