开发者

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. 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.
  2. value - Any string long or short but enclosed in double quotes ("").
  3. pair - (key:value) A key and value combined by colon just like in javascript objects.
  4. operator - (& or |) Simply indicating 'AND' or '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.

$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

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. Therefor I feel there is something wrong in the :oneChar reg ex. Kindly help me in resolving this issue.


I see at least one error in the second regular expression ($regs[':oneChar']). There is a forward slash in it. And it is conflicting with the forward slashes being used in preg_match as delimiters. Use preg_match("@^{$regs[':value']}@", $query, $matches) instead.

You may also need to use preg_quote on the input string.

$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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜