简体   繁体   English

用PHP解析带有正则表达式的块

[英]Parsing Blocks with Regular Expression in PHP

I am stuck with parsing a string containing key-value pairs with operators in between (like the one below) in PHP. 我一直坚持用PHP解析一个包含键值对的字符串,并在它们之间使用运算符(如下所示)。 I am planning to user regex to parse it (I am not good at it though). 我正计划使用正则表达式来解析它(尽管我不太擅长)。

key: "value" & key2 : "value2" | title: "something \\"here\\"..." &( key: "this value in paranthesis" | key: "another value")

Basically the units in the above block are as follows 基本上,以上块中的单位如下

  1. key - Anything that qualifies to be a javascript variables. key -任何符合条件的JavaScript变量。
  2. value - Any string long or short but enclosed in double quotes (""). value任何长或短但用双引号(“”)引起的字符串。
  3. pair - ( key:value ) A key and value combined by colon just like in javascript objects. pair -( key:value )与冒号组合的键和值,就像在javascript对象中一样。
  4. operator - ( & or | ) Simply indicating 'AND' or 'OR'. operator -( &| )仅表示“ AND”或“ OR”。

There can be multiple blocks nested within prantheses ( and ) . 括号()可以嵌套多个块。

Being inspired from Matt (http://stackoverflow.com/questions/2467955/convert-javascript-regular-expression-to-php-pcre-expression) I have used the following regular expressions. 从Matt(http://stackoverflow.com/questions/2467955/convert-javascript-regular-expression-to-php-pcre-expression)的启发中,我使用了以下正则表达式。

$regs[':number'] = '(?:-?\\b(?:0|[1-9][0-9]*)(?:\\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\\b)';
$regs[':oneChar'] = '(?:[^\\0-\\x08\\x0a-\\x1f\"\\\\]|\\\\(?:[\"/\\\\bfnrt]|u[0-9A-Fa-f]{4}))';
$regs[':string'] = '(?:\"'.$regs[':oneChar'].'*\")';
$regs[':varName'] = '\\$(?:'.$regs[':oneChar'].'[^ ,]*)';
$regs[':func'] = '(?:{[ ]*'.$regs[':oneChar'].'[^ ]*)';
$regs[':key'] = "({$regs[':varName']})";
$regs[':value'] = "({$regs[':string']})";
$regs[':operator'] = "(&|\|)";
$regs[':pair'] = "(({$regs[':key']}\s*:)?\s*{$regs[':value']})";

if(preg_match("/^{$regs[':value']}/", $query, $matches))
{
  print_r($matches);
}

When executing the above, PHP throws an error near the IF condition 执行上述操作时,PHP在IF条件附近引发错误

Warning: preg_match() [function.preg-match]: Unknown modifier '\' in /home/xxxx/test.xxxx.com/experiments/regex/index.php on line 23

I have tried to preg_match with :string and :oneChar but still I get the same error. 我试图与:string和:oneChar进行preg_match匹配,但仍然收到相同的错误。 Therefor I feel there is something wrong in the :oneChar reg ex. 因此,我觉得:oneChar reg ex存在问题。 Kindly help me in resolving this issue. 请帮助我解决此问题。

I see at least one error in the second regular expression ($regs[':oneChar']). 我在第二个正则表达式($ regs [':oneChar'])中看到至少一个错误。 There is a forward slash in it. 其中有一个正斜杠。 And it is conflicting with the forward slashes being used in preg_match as delimiters. 它与preg_match中用作分隔符的正斜杠冲突。 Use preg_match("@^{$regs[':value']}@", $query, $matches) instead. 请改用preg_match("@^{$regs[':value']}@", $query, $matches)

You may also need to use preg_quote on the input string. 您可能还需要在输入字符串上使用preg_quote。

$query = preg_quote($query, '/');

Beyond that, I would run each of your regular expressions one at a time to see which one is throwing the error. 除此之外,我会一次运行每个正则表达式,以查看哪个正引发错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM