I have a method that evaluates arithmetic expressions from a string, but it is very dependant on the format (all tokens should be separated with one space):
2 + 2
-2 + ( 7 / 5 ) * 6
and so on. So I want to add method Normalize(), which would, well, normalize input string to the appropriate format (delete extra spaces, add necessacy spaces and handle uniry minus) and signal of errors, if any.
Initially, I wanted to use regular expression to check if line is actually an expression, but this only half of job. What is the best way to normalize the string in this case?
You do not need a “normalizer”. If you use regular expressions, take whitespaces into account when writing the pattern. But regex is not the best solution to this problem. The usual approach is to write a parser, check out for example JavaCC.