简体   繁体   English

涉及浮点数的表达式计算器的正则表达式

[英]Regular Expression for Expression Calculator involving Floating Point Numbers

I am making an expression calculator in Android so i wanted to check whether the string qualifies as a valid expression before actually computing the answer. 我正在用Android开发一个表达式计算器,因此我想在实际计算答案之前检查字符串是否符合有效表达式的要求。

I tried this regex in Java: 我在Java中尝试过此正则表达式:

^\\s*([-+]?)(\\d+)(?:\\s*([-+ /])\\s ((?:\\s[-+])?\\d+)\\s*)+$ ^ \\ s *([-+]?)(\\ d +)(?:\\ s *([-+ /])\\ s ((?:\\ s [-+])?\\ d +)\\ s *)+ $

But the problem with it is that, it can only match expressions which don't involve floating point numbers. 但问题在于,它只能匹配不涉及浮点数的表达式。

Can somebody guide me and provide me a proper regex for the same? 有人可以指导我,并为我提供适当的正则表达式吗?

Some example strings it should match: 它应该匹配的一些示例字符串:

3.2+4.6-9.001*1 3.2 + 4.6-9.001 * 1

4+2+9.0-89 4 + 2 + 9.0-89

590 + 9.077236 + 3673.126 + 34787.3284 - 0.99347 * 872367 590 + 9.077236 + 3673.126 + 34787.3284-0.99347 * 872367

I'd advise not doing it as a regular expression. 我建议不要将其作为正则表达式。 Can you honestly tell me you can read and debug that string? 老实说,您可以读取和调试该字符串吗? If you wanted to add features like exponentiation, could you alter it and be confident in it? 如果您想添加幂运算等功能,是否可以更改它并对此充满信心? Of course not. 当然不是。 Use a real parser framework (a tokenizer is even part of the SDK) and do validation over the token string. 使用真实的解析器框架(令牌生成器甚至是SDK的一部分)并通过令牌字符串进行验证。 You'll need to do that to effectively evaluate the expression anyway. 无论如何,您都需要这样做才能有效地评估表达式。

A regular expression is not suited for parsing a mathematical expression, this is because a regular expression is a language which corresponds to a DFA/NFA state machine where the language does not maintain how it reached a particular state. 正则表达式不适合解析数学表达式,这是因为正则表达式是一种与DFA / NFA状态机相对应的语言,在该状态机中,语言不保持其达到特定状态的方式。 A context free language should instead be used to parse a mathematical expression. 相反,应该使用上下文无关的语言来解析数学表达式。

In a context free language allows a parser to maintain information about how it entered the state during parsing, since a mathematical expression has a recursive tree-like structure, a context free language will also allow you to express operator precedence and check for matching brackets (which a regex cannot). 在上下文无关的语言中,由于数学表达式具有类似树的递归结构,因此上下文无关的语言允许解析器维护在解析过程中如何进入状态的信息,上下文无关的语言还允许您表达运算符的优先级并检查匹配的括号(正则表达式不能)。

For creating a parser you can either write one yourself by hand in the form of a recursive decent parser (there are many examples of how to write one for parsing expressions), or use a tool or a framework to generate the parser for you. 对于创建解析器,您可以以递归的体面解析器的形式手动编写自己(有许多示例说明如何为解析表达式编写代码),也可以使用工具或框架为您生成解析器。

You can still make use of regular expressions in order to recognise numbers and operators which you can then feed to your parser, which makes the parsing process simpler. 您仍然可以使用正则表达式来识别数字和运算符,然后将它们输入解析器,这使解析过程更加简单。 The phase of recognising the "words" of a language is called lexical analysis and the phase of recognising the structure of the language is the syntactical phase. 识别语言的“单词”的阶段称为词法分析,而识别语言的结构的阶段称为句法阶段。

This is a very brief description of parsing but I hope it helps. 这是对解析的非常简短的描述,但希望对您有所帮助。

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

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