Kohnarik
Kohnarik

Reputation: 377

Java extract multiple substrings out of string

I have made a Java Interpreter, in which the user may type in operations like the following:

$x+23/$y+9*$z

$x , $y, $z are variables previously defined.

With another piece of code, I will replace those variables with their respective values and then I want to put the string back together.

Example:

$x = 1 $y = 2 $z = 3

So the operation will finally look like this:

1+23/2+9*3

How do I efficiently extract those variables from the main String and fit them back in, given the circumstances that the user might perform operations of different length and order?

Upvotes: 0

Views: 376

Answers (1)

Artur Opalinski
Artur Opalinski

Reputation: 1092

I think you should parse your input string completely to separate it into meaningful tokens, and you should not rely on e.g. search-and-replace.

Search-and-replace does not take the whole context into account, so even if there are syntax errors, or multi-character variable names - it will blindly replace part of user input data. E.g. consider that your user has defined a variable named 'e' and then he writes a number in exponential format like: 1e-2 (for 0.01). Obviously the 'e' in the number should not be treated as the above mentioned variable 'e' in this context.

So to make it orderly: define for yourself what your tokens are. It seems to me they are:

  1. Floating point numbers (an integral number can be considered a special case of it, if you do not want to consider the type of user-provided expressions). Think if you want to accept exponential format for numbers as well (e.g. 1e-2 for 0.01)
  2. Operators: +,*,/ (and perhaps others?)
  3. Variable names (starting with $, perhaps multicharacter? perhaps case-(in)sensitive?)

After defining the tokens, define your grammar. E.g. in the so called Polish Notation writing "2 3 +" is a representation for "2+3" (Polish Notation is easy for evaluating expressions using stack, it frees you from analyzing parentheses and different priorities of your operators).

Then parse user input to extract tokens and to analyze their relation according to your grammar. Otherwise you will get very many corner cases for which your program does not work well, including user typos that you will be unable to delimit (you should provide a 'syntax error' message in such cases.

Upvotes: 2

Related Questions