简体   繁体   English

带括号的数学运算的正则表达式

[英]Regular expression for math operations with parentheses

In java, I'm trying to write a regular expression that will match a unit within a mathematical expression, ie things that are between operators 在Java中,我试图编写一个与数学表达式中的一个单元匹配的正则表达式,即运算符之间的内容

What I mean is, in an expression like 1 + [1 + 2], the regex should match the first 1 and then the [1 + 2]. 我的意思是,在类似1 + [1 + 2]的表达式中,正则表达式应匹配前1个,然后匹配[1 + 2]。

What I have is *[([-+]?\\d+(\\.\\d+)?)(\\[.+\\])] * 我所拥有的是* [([-+]?\\ d +(\\。\\ d +)?)(\\ [。+ \\])] *

Of which ([-+]?\\d+(\\.\\d+)?) is supposed to match any number and 其中([-+]?\\ d +(\\。\\ d +)?)应该匹配任何数字,并且

(\\[.+\\]) (\\ [。+ \\])

Is supposed to match something inside parentheses, but it isn't working...it's matching things like ']' and ' ' for some reason. 应该匹配括号内的内容,但是不起作用...由于某种原因,它匹配']'和''之类的内容。

Any help would be great :) 任何帮助将是巨大的:)

Unfortunately this is part of an exercise and so I can only use the basic java library...It's also meant to be an exercise in regular expressions. 不幸的是,这是练习的一部分,因此我只能使用基本的Java库...这也意味着它是正则表达式中的练习。 Am I missing something basic here? 我在这里缺少基本的东西吗?

You can't find matching parentheses with regular expressions. 您找不到带有正则表达式的匹配括号。 This is a consequence of the pumping lemma for regular languages (the mathematical objects that regexes represent) not holding for languages with matched open/close parens. 这是常规语言正则表达式表示的数学对象)的抽水引理引起的,而对于具有匹配的打开/关闭括号的语言,则不成立。

You'll need a context-free parser at the least. 您至少需要一个无上下文解析器。 Those can be built with ANTLR or JavaCC. 这些可以使用ANTLR或JavaCC构建。

You are not going to be able to accomplish this with a regular expression. 您将无法使用正则表达式来完成此操作。 An arithmetic expression can be described using a BNF grammar which can be used to generate a parser using a tool like JavaCC or ANTLR. 可以使用BNF语法描述算术表达式,该语法可以使用JavaCC或ANTLR等工具生成解析器。

Here is an expression parser that I implemented using JavaCC: 这是我使用JavaCC实现的表达式解析器:

http://dev.eclipse.org/viewcvs/viewvc.cgi/org.eclipse.sapphire/plugins/org.eclipse.sapphire.modeling/src/org/eclipse/sapphire/modeling/el/parser/internal/ExpressionLanguageParser.jj?view=markup&revision=1.6&root=Technology_Project http://dev.eclipse.org/viewcvs/viewvc.cgi/org.eclipse.sapphire/plugins/org.eclipse.sapphire.modeling/src/org/eclipse/sapphire/modeling/el/parser/internal/ExpressionLanguageParser。 jj?view = markup&revision = 1.6&root = Technology_Project

The source is EPL. 来源是EPL。 If you look around that CVS location, you will also find AST classes and evaluation logic. 如果环顾CVS所在的位置,您还将找到AST类和评估逻辑。 The implementation is derived from the expression language defined for JSP/JSF specs. 该实现源自为JSP / JSF规范定义的表达语言。

I'd echo what the other answerers have stated (regex's won't suffice for parsing arithmetic expressions), but recommend parboiled over ANTLR. 我会回应其他回答者所说的内容(正则表达式不足以解析算术表达式),但建议将其反编译为ANTLR。

They even have a set of calculator examples you could start with. 他们甚至提供了一组您可以开始使用的计算器示例

i released an expression evaluator based on Dijkstra's Shunting Yard algorithm, under the terms of the Apache License 2.0 : 我根据Apache License 2.0的条款发布了基于Dijkstra的Shunting Yard算法的表达式评估器:

http://projects.congrace.de/exp4j/index.html http://projects.congrace.de/exp4j/index.html

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

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