Reputation: 43
I am a beginner with regexes, and I got this assignment to filter parameters with -
and --
joined with multiple alnums ending with =
or without it. So positive catch would be --input=
or -help
Here is my custom regex
^--((\w|-)*)(=([^\s]+))?$
the whole problem is that it does not catch =
or -
. For e.g. -input
or --input=
is not caught and I have no idea why.
Upvotes: 2
Views: 203
Reputation: 80639
Use a ?
for optional second hyphen. The pattern would become:
^--?([\w-]+)(?:=(\S*))?$
PS: [^\s]
is same as \S
.
Changing +
to *
allows for empty parameter values. When you use a +
, the pattern expects =
character to be followed by some value as well.
Upvotes: 1