Answer the question
In order to leave comments, you need to log in
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
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).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question