[英]Regular expression for validating math equation
我正在尝试构建一个验证数学方程式的正则表达式。 方程式本身非常简单,我希望制作一个英文可读的方程式,以后我将返回真或假。 一个例子就是这样。
((1和2)或3)
在这个例子中,我将换出任何带有true或false的数字。 我也会用“&&”替换“和”,用“||”替换“或” 为了使用PHP运行方程式。 对此的回应最终将是真实的,或者是错误的。
示例最终等式看起来像这样:
((true && true)|| true)
以下是一些应该被认为有效的例子。
(1或2或3)
((1和2和3)或(4和5))
所以,我的问题分为两部分。
这可以在php中完成(使用PCRE引擎)。
以下只是一个例子。
您可以注释掉错误检查,然后插入边界构造
在正则表达式周围,使它明确通过/失败。
最大的问题不是递归,而是定义内容边界
条件。 我差点把它煮熟了。 这些检查必须
保持任何你如何做,状态,堆栈......,它都是一样的。
(这个正则表达式是使用RegexFormat 6构建和测试的)
样本输入:
((( (1 and 2 and 3) or (9) or ( ( 4 and 5)) and 5 ) and 7) )
测试输出:
** Grp 0 - ( pos 0 , len 64 )
((( (1 and 2 and 3) or (9) or ( ( 4 and 5)) and 5 ) and 7) )
** Grp 1 - ( pos 1 , len 62 )
(( (1 and 2 and 3) or (9) or ( ( 4 and 5)) and 5 ) and 7)
** Grp 2 - NULL
** Grp 3 - NULL
** Grp 4 - NULL
正则表达式:
5/29所有表格:
空表格( )
不允许
空表格) (
不允许
表格) and (
好的
表格) and 2 and (
好的
表格( 1 and 2 )
确定
表格( 1 )
好的
表格) and 2 )
确定
表格( 1 and (
好的
表格( whitespace (
或) whitespace )
确定
# (?s)(?:\(((?!\s*\))(?&core))\)|\s*([()]))(?(DEFINE)(?<core>(?>(?&content)|\((?:(?!\s*\))(?&core))\)(?!\s*\())+)(?<content>(?>(?<=\))\s*(?:and|or)\s*(?=\()|(?<=\))\s*(?:(?:and|or)\s+\d+)+\s*(?:and|or)\s*(?=\()|(?<=\()\s*\d+(?:(?:\s+(?:and|or)\s+)?\d+)*\s*(?=\))|(?<=\))\s*(?:(?:and|or)\s+\d+)+\s*(?=\))|(?<=\()\s*(?:\d+\s+(?:and|or))+\s*(?=\()|\s+)))
# //////////////////////////////////////////////////////
# // The General Guide to 3-Part Recursive Parsing
# // ----------------------------------------------
# // Part 1. CONTENT
# // Part 2. CORE
# // Part 3. ERRORS
(?s) # Dot-All modifier (used in a previous incarnation)
(?:
# ( # (1), Take off CONTENT (not used here)
# (?&content)
# )
# | # OR
\( # Open Paren's
( # (1), parens CORE
(?! \s* \) ) # Empty form '( )' not allowed
(?&core)
)
\) # Close Paren's
| # OR
\s*
( # (2), Unbalanced (delimeter) ERRORS
# - Generally, on a whole parse, these
# are delimiter or content errors
[()]
)
)
# ///////////////////////
# // Subroutines
# // ---------------
(?(DEFINE)
# core
(?<core> # (3)
(?>
(?&content)
|
\( # Open Paren's
(?:
(?! \s* \) ) # Empty form '( )' not allowed
(?&core)
)
\) # Close Paren's
(?! \s* \( ) # Empty form ') (' not allowed
)+
)
# content
(?<content> # (4)
(?>
(?<= \) ) # Form ') and ('
\s*
(?: and | or )
\s*
(?= \( )
|
(?<= \) ) # Form ') and 2 and ('
\s*
(?:
(?: and | or )
\s+
\d+
)+
\s*
(?: and | or )
\s*
(?= \( )
|
(?<= \( ) # Form '( 1 and 2 )'
\s*
\d+
(?:
(?:
\s+
(?: and | or )
\s+
)?
\d+
)*
\s*
(?= \) )
|
(?<= \) ) # Form ') and 2 )'
\s*
(?:
(?: and | or )
\s+
\d+
)+
\s*
(?= \) )
|
(?<= \( ) # Form '( 1 and ('
\s*
(?:
\d+
\s+
(?: and | or )
)+
\s*
(?= \( )
|
\s+ # Interstitial whitespace
# '( here (' or ') here )'
)
)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.