preg匹配所有提取功能名称和参数的名称

发布于 2025-01-24 02:05:34 字数 946 浏览 0 评论 0原文

我有此字符串:

not(matches(#[Sécurité][Alarme][Mode]#, ["Desarmer",{"action":"Desarmer Sys"}]), result).

我想提取函数的名称:不是

,我也想提取功能的参数:

matches(#[Sécurité][Alarme][Mode]#, ["Desarmer",{"action":"Desarmer Sys"}])
result

我尝试过,但它不起作用:

~[\w]+(?:\w+(\([^()]*+(?:(?1)[^()]*)*+\)))+~u

它可以正常工作第一句话,但是,如果我回想起参数的功能 “匹配(#[sécurité] [alarme] [mode]#,[“ desarmer”,{“ action”:“ desarmer sys”}])”我无法获得正确的参数。

这是所有不同参数的列表:

  • 匹配(#[sécurité] [armare] [mode]#,[“ desarmer”,“ desarmer sys”]),
  • #[sécurité] [armare] [armare] [mode]#,
  • “ desarmer” ,
  • [“ Test11”,“ Test-12”],
  • {“ T1”:“ Test-21”,“ T2”:“ Test22”},
  • [“ Test11”,“ Test-12”,{“ T2”: test21“,” T2“:” test-22“}],
  • #test#,
  • [#[sécurité] [armare] [mode]#”,“ test12”,{“ t3”:“#[sécurité] ] [模式]#,“ T2”:“ test-22”}]]

I have this string:

not(matches(#[Sécurité][Alarme][Mode]#, ["Desarmer",{"action":"Desarmer Sys"}]), result).

And I want to extract the name of the function: not

And I want to extract the parameters of function, too:

matches(#[Sécurité][Alarme][Mode]#, ["Desarmer",{"action":"Desarmer Sys"}])
result

I tried with this but it does not work:

~[\w]+(?:\w+(\([^()]*+(?:(?1)[^()]*)*+\)))+~u

It works fine for the first sentence but, if I recall for function on parameter
"matches(#[Sécurité][Alarme][Mode]#, ["Desarmer",{"action":"Desarmer Sys"}])" I can't obtain the right parameter.

This is a list of all different parameters:

  • matches(#[Sécurité][Alarme][Mode]#, ["Desarmer","Desarmer Sys"]),
  • #[Sécurité][Alarme][Mode]#,
  • "Desarmer",
  • ["test11","test-12"],
  • {"t1":"test-21","t2":"test22"},
  • ["test11","test-12",{"t2":"test21","t2":"test-22"}],
  • #test#,
  • ["#[Sécurité][Alarme][Mode]#","test12",{"t3":"#[Sécurité][Alarme][Mode]#","t2":"test-22"}]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘虞初梦 2025-01-31 02:05:34

我认为对各种元素进行了更仔细的描述(特别是为了避免在字符串内的圆括号之类的陷阱),:

$pattern = <<<'REGEX'
~
# subpatterns definition

(?(DEFINE)
    (?<dqstring> " [^\\"]*+ (?s: \\ . [^\\"]* )*+ " )
    (?<bcontent> [^()"]*+ (?: \g<dqstring> [^()"]* )*+ )
    (?<rbrackets> \( \g<bcontent> (?: \g<rbrackets> \g<bcontent> )*+ \) )
    (?<pcontent> [^,()"]* (?: (?: \g<dqstring> | \g<rbrackets> ) [^,()"]* )* )
)

# main pattern

    \G [(,] \s* (?<param> \g<pcontent> ) (*:param)
|
    (?<funcName> \w+ ) (*:funcName)
~xu
REGEX;

$str = 'not(matches(#[Sécurité][Alarme][Mode]#, ["Desarmer",{"action":"Desarmer Sys"}]), result)';

preg_match_all($pattern, $str, $matches, PREG_SET_ORDER);

foreach($matches as $m) {
    $type = $m['MARK'];
    echo "$type:  ", $m[$type], PHP_EOL;
}

演示

I think a more careful description of the various elements is in order (in particular to avoid pitfalls like round brackets inside a string), :

$pattern = <<<'REGEX'
~
# subpatterns definition

(?(DEFINE)
    (?<dqstring> " [^\\"]*+ (?s: \\ . [^\\"]* )*+ " )
    (?<bcontent> [^()"]*+ (?: \g<dqstring> [^()"]* )*+ )
    (?<rbrackets> \( \g<bcontent> (?: \g<rbrackets> \g<bcontent> )*+ \) )
    (?<pcontent> [^,()"]* (?: (?: \g<dqstring> | \g<rbrackets> ) [^,()"]* )* )
)

# main pattern

    \G [(,] \s* (?<param> \g<pcontent> ) (*:param)
|
    (?<funcName> \w+ ) (*:funcName)
~xu
REGEX;

$str = 'not(matches(#[Sécurité][Alarme][Mode]#, ["Desarmer",{"action":"Desarmer Sys"}]), result)';

preg_match_all($pattern, $str, $matches, PREG_SET_ORDER);

foreach($matches as $m) {
    $type = $m['MARK'];
    echo "$type:  ", $m[$type], PHP_EOL;
}

demo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文