我如何使用正则表达式获取流程参数?

how do i get the process arguments using regex?

我有一个这样的过程参数:

-a testa testaa testaaa -b testb testbb -c test-c -d

我想要这样的结果:

-a testa testaa testaaa

-b testb testbb

-c test-c

-d

这个正则表达式应该可以解决问题:

(?<=( |^))-.*?(?=(?<=( |^))-|$)

其中:

  • (?<=( |^))- 标识每个参数的开始,即 -
  • .* 匹配 -
  • 之后的所有字符
  • ? 使这个匹配不那么贪婪
  • (?=(?<=( |^))-|$) 包含与第一个要点 (?<=( |^))- 相同的正则表达式。 $ 表示字符串的结尾,所以用更简单的话来说: (?=(start of argument) OR (end of string)) .

(参考Regex Cheat Sheet