I need to modify this regex to find multiple group matches:
(?:--)(?<key>[^\\s=]+)(?:(?<assign> *[ =] *)(?! --)(?<value>"[^"]*"|\\S+))?
In Java:
"(?:--)(?<key>[^\\\\s=]+)(?:(?<assign> *[ =] *)(?! --)(?<value>\\"[^\\"]*\\"|\\\\S+))?"
This matches the following correctly:
--key=value
--key=--value
--key value
--flag
--key="--value"
--key "--value"
--key=value --foo=bar
--key=value --foo=bar --flag
But it fails if --flag
comes before any other options:
--key=value --flag --foo=bar
I've been trying to modify the negative lookahead between the assign
and value
capture groups without success so far. The value
captured for flag
ends up being --foo=bar
instead of null
.
Any expert recommendations on how to solve this?
I managed to fix the regex. The website https://regexr.com/ was invaluable.
The fixed regex is:
(?<prefix>--)(?<key>[^\\s=]+)(?:(?! --)(?<assign> *[ =] *)(?! --)(?<value>"[^"]*"|\\S+))?
Here's the Java class and unit test:
https://gist.github.com/kirklund/845baf340a1999a57db9e59e6ba40ce0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.