infix表达式:假和真或真和(假或假)
我想要一个包含元素的数组:
假“、”和“、”真“、”或“、”真“、”和“、"(”、“假”、“或”、“假”、“假”或“不”、“真”、“真")
我不能使用空格作为分隔符,因为括号不能与下一个分隔,或者使用空格进行true/false。
发布于 2014-09-05 08:20:20
您可以尝试下面的代码,它使用正的前瞻性和前瞻性,
<?php
$yourstring = "false and true or true and (false or false or not true)";
$regex = '~\s|(?<=\()|(?=\))~';
$splits = preg_split($regex, $yourstring);
print_r($splits);
?>输出:
Array
(
[0] => false
[1] => and
[2] => true
[3] => or
[4] => true
[5] => and
[6] => (
[7] => false
[8] => or
[9] => false
[10] => or
[11] => not
[12] => true
[13] => )
)发布于 2014-09-05 08:22:37
既然您将其标记为regex,那么我假设您希望使用正则表达式?
这个怎么样?
(false|true|\(|\)|and|or|not)例如
$input = "false and true or true and (false or false or not true)";
$regex = '/(false|true|\(|\)|and|or|not)/';
preg_match_all($regex, $input, $tokens);
var_dump($tokens);https://stackoverflow.com/questions/25681402
复制相似问题