V
V
Vadim Dunkin2016-12-18 00:49:50
Java
Vadim Dunkin, 2016-12-18 00:49:50

How to make a regex check to check the entered arithmetic expression?

Hello everyone, in general, I have such a problem, how can I check the entered expression for correct input, let's say the user entered the following expression on the command line:
5/7 + 4 - 5{5/8} / 5/8 * 8 + -6
Here I am it is necessary to check this expression for correctness, that is, for one fraction I made up the expression:
String regexp = "(\\-)?\\d+((\\/\\d+)|(\\{\\d+\\/\\ d+\\}))?";
That is, I took into account only one fraction, but now I need to check for the correctness of the entire expression, taking into account the signs of arithmetic operations
. Who cares - there is a Java code where this is written .. https://bitbucket.org/13oit18k/rational/src/96a724 ...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
amorphine, 2016-12-18
@amorphine

Break the entire expression into lines. Let + and - act as a separator, provided that they are not surrounded by () and other brackets. Next, we break the expressions in brackets on the same basis as long as there is something to break. We check the correctness of small parts (these will be fractions and products).

A
Andryukha, 2016-12-18
@syrov

A regular expression is essentially a state machine, it cannot parse such a string, since it has a count of {} quotes. This is a context free grammar.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question