【问题标题】:how do i get the process arguments using regex?如何使用正则表达式获取流程参数?
【发布时间】: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


    【解决方案1】:

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

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

    地点:

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

    (参考Regex Cheat Sheet

    【讨论】:

    • 此匹配找不到“参数值包含-”,如-c test-c。
    • 请检查此编辑
    猜你喜欢
    • 1970-01-01
    • 2011-10-14
    • 1970-01-01
    • 1970-01-01
    • 2015-10-21
    • 1970-01-01
    • 1970-01-01
    • 2017-02-15
    相关资源
    最近更新 更多