Home > database >  preg match all for extract name of function and parameter
preg match all for extract name of function and parameter

Time:04-25

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

And i wont extract the name of the function:

  • not

And i wont extract the parammeter of function:

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

I tried whit this but does not work: ~[\w] (?:\w (([^()] (?:(?1)[^()])* ))) ~u

CodePudding user response:

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

CodePudding user response:

It's work fine for the first sentence but, if i recal for function on parameter "matches(#[Sécurité][Alarme][Mode]#, ["Desarmer",{"action":"Desarmer Sys"}])" i can't uptain the right parameter.

This is a list of all different parameter:

  • 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"}]
  • Related