如何在php中标记以下中缀表达式?

如何在php中标记以下中缀表达式?,php,regex,Php,Regex,中缀表达式:false and true或true and(false or false or not true) 我想要一个包含元素的数组: “假”、“和”、“真”、“或”、“真”、“和”(“假”、“假”、“或”、“假”、“或”、“不”、“真”) 我不能使用空格作为分隔符,因为括号不能用空格与下一个或后续的true/false分隔 您可以尝试下面的代码,该代码使用正向向前看和向后看 <?php $yourstring = "false and true or true and (fals

中缀表达式:false and true或true and(false or false or not true)

我想要一个包含元素的数组:

“假”、“和”、“真”、“或”、“真”、“和”(“假”、“假”、“或”、“假”、“或”、“不”、“真”)


我不能使用空格作为分隔符,因为括号不能用空格与下一个或后续的true/false分隔

您可以尝试下面的代码,该代码使用正向向前看和向后看

<?php
$yourstring = "false and true or true and (false or false or not true)";
$regex = '~\s|(?<=\()|(?=\))~';
$splits = preg_split($regex, $yourstring);
print_r($splits);
?>
既然您将其标记为,我想您应该使用正则表达式

这个怎么样

(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);

我认为您试图做的是构建一个函数来计算布尔表达式

你可以这样做,给你一个抽象的树

([a-zA-Z0-9]+)|(\()|(\))
您可以使用此正则表达式。这将给出一个包含“”等值的列表,这些值未定义,您可以使用下划线\ux.compact()将其删除

见演示


请分享任何失败的尝试。并解释您实际想要做什么(列出各种
true
false
标记似乎不是很有用),或者您想要如何利用它。(否则请不要再问关于如何真正解析和解释完全不同的东西的后续问题。)非常感谢阿维纳什。我正是在找这个:)
([a-zA-Z0-9]+)|(\()|(\))