【发布时间】:2022-01-29 21:32:37
【问题描述】:
我有一个这样的进程参数:
-a testa testaa testaaa -b testb testbb -c test-c -d
我想得到如下结果:
-a testa testaa testaaa
-b testb testbb
-c 测试-c
-d
【问题讨论】:
标签: regex command-line-arguments
我有一个这样的进程参数:
-a testa testaa testaaa -b testb testbb -c test-c -d
我想得到如下结果:
-a testa testaa testaaa
-b testb testbb
-c 测试-c
-d
【问题讨论】:
标签: regex command-line-arguments
这个正则表达式应该可以解决问题:
(?<=( |^))-.*?(?=(?<=( |^))-|$)
地点:
(?<=( |^))- 标识每个参数的开头,即 -
.* 匹配 - 之后的所有字符
? 使这种匹配不那么贪婪(?=(?<=( |^))-|$) 包含与第一个要点 (?<=( |^))- 相同的正则表达式。 $ 表示字符串的结尾,简单来说:(?=(start of argument) OR (end of string))。【讨论】: