首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从string Parsing \ php解析参数

从string Parsing \ php解析参数
EN

Stack Overflow用户
提问于 2017-03-23 15:13:18
回答 1查看 134关注 0票数 1

我在从字符串解析参数时遇到了问题。

参数由以下内容定义:

  • 可以用短的或长的符号写,p.ex:-a / -- long
  • 字符范围从a-z0-9 (缩写为-z0-9)和a-z0-9(用于长符号,p.ex: p.ex)。
  • 可以有一个值,但不必,p.ex:-a test /-aaaa
  • 可以有多个参数,而不用在引号中,p.ex:-a val1 val2 (应该作为一个组捕获: value = "val1 val2")
  • 可以有自定义文本引号--自定义“这里可以承受一切,”测试测试:( "“)
  • 参数可以有一个"!“!-测试测试/ !-a
  • 值可以在-a值-with-dash中具有"-“。

所有这些参数都是一个长字符串,p.ex:

-a val1 ! -b val2 --other "string with crazy -a --test stuff inside" --param-with-dash val1 val2 -test value-with-dash ! -c -d ! --test

-编辑

也是--param value-with-dash

-结束编辑

这是我所能得到的最接近的:

https://regex101.com/r/3aPHzp/1

/(?:(?P<inverted>\!) )?(?P<names>\-{1,2}\S+)($| (?P<values>.+(?=(?: [\!|\-])|$)))/U

不幸的是,当谈到引号中的自由文本值时,它就会中断。当没有值的参数后面跟着下一个参数时。

(我试图解析iptables的输出-保存,以防您被插入。另外,也许我以前可以用另一种奇特的方式拆分字符串,以避免hugh正则表达式,但我没有看到)。

非常感谢您的帮助!

--最终解决方案--

>= 5.6

(?<inverted>!)?\s*(?<name>--?\w[\w-]*)\s*(?<values>(?:\s*(?:\w\S*|["'](?:[^"'\\]*(?:\\.[^"'\\]*)*)['"]))*)\K

演示:https://regex101.com/r/xSfgxP/1

PHP < 5.6

(?<inverted>\!)?\s*(?<=(?:\s)|^)(?<name>\-{1,2}\w[\w\-]*)\s+(?<value>(?:\s*(?:\w\S*|["'](?:[^"'\\]*(?:\\.[^"'\\]*)*)['"]))*)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-23 16:19:04

RegEx:

代码语言:javascript
复制
(?<inverted>!)?\s*(?<name>--?\w[\w-]*)\s*(?<values>(?:\s*(?:\w\S+|["'](?:[^"'\\]*(?:\\.[^"'\\]*)*)['"]))*)\K

现场演示 (更新)

细目

代码语言:javascript
复制
 (?<inverted> ! )?             # (1) Named-capturing group for inverted result
 \s*                           # Match any spaces
 (?<name> --? \w [\w-]* )      # (2) Named-capturing group for parameter name
 \s*                           # Match any spaces
 (?<values>                    # (3 start) Named capturing group for values
      (?:                           # Beginning of a non-capturing group (a)
           \s*                      # Match any spaces
           (?:                      # Beginning of a non-capturing group (b)
                \w\S+                   # Match a [a-zA-Z0-9_] character then any non-whitespace characters
             |                          # Or
                ["']                    # Match a qoutation mark
                (?:                     # Beginning of a non-capturing group (c)
                     [^"'\\]*               # Match anything except `"`, `'` or `\`
                     (?: \\ . [^"'\\]* )*   # Match an escaped character then anyhthing except `"`, `'` or `\` as much as possible
                )                       # End of non-capturing group (c)
                ['"]                    # Match qutation pair
           )                        # End of non-capturing group (b)
      )*                            # Greedy (a), end of non-capturing group (a)
 )                             # (3 end)
 \K                            # Reset allocated memory of all previously matched characters

PHP代码:

代码语言:javascript
复制
<?php 
    
$str = '-a val1 ! -b val2 --custom "string :)(#with crazy -a --test stuff inside" --param-with-dash val1 val2 -c ! -d ! --test';
$re = <<< 'RE'
~(?<inverted>!)?\s*(?<name>--?\w[\w-]*)\s*(?<values>(?:\s*(?:\w\S+|["'](?:[^"'\\]*(?:\\.[^"'\\]*)*)['"]))*)\K~
RE;

preg_match_all($re, $str, $matches, PREG_SET_ORDER);
print_r(array_map('array_filter', $matches));

输出:

代码语言:javascript
复制
Array
(
    [0] => Array
        (
            [name] => -a
            [2] => -a
            [values] => val1
            [3] => val1
        )

    [1] => Array
        (
            [inverted] => !
            [1] => !
            [name] => -b
            [2] => -b
            [values] => val2
            [3] => val2
        )

    [2] => Array
        (
            [name] => --custom
            [2] => --custom
            [values] => "string :)(#with crazy -a --test stuff inside"
            [3] => "string :)(#with crazy -a --test stuff inside"
        )

    [3] => Array
        (
            [name] => --param-with-dash
            [2] => --param-with-dash
            [values] => val1 val2
            [3] => val1 val2
        )

    [4] => Array
        (
            [name] => -c
            [2] => -c
        )

    [5] => Array
        (
            [inverted] => !
            [1] => !
            [name] => -d
            [2] => -d
        )

    [6] => Array
        (
            [inverted] => !
            [1] => !
            [name] => --test
            [2] => --test
        )

)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42980062

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档