REGEX 从控制台命令获取标志



我正在尝试获取一个可以提取字符串中的标志和值的正则表达式。基本上,我需要能够获取这样的字符串:

 command -aparam -b"Another "quoted" param" -canother one here

并捕获数据:

a  param
b  Another "quoted" param
c  another one here

这是我到目前为止的 Java 正则表达式:

(?<= -w)(?:(?=")(?:(?:")([^"\]*(?:\.[^"\]*)*)(?="))|.*?( -w|$))?

但还不太有效。有什么建议吗?

建议使用可用的 CLI 解析器之一。例如,来自雅加达的 CLI,或者更好的是 args4j。

使用split方法将字符串标记为命令及其参数,

String input = "command -aparam -b"Another "quoted" param" -canother one here ";
String[] cmds = input.split("\s*-(?=\w)");

最新更新