Reputation: 187
For example, if the expression can have + - / * as operators, and floating point numbers
and the input is "3.5+6.5-4.3*4/5"
How should write a program that returns "3.5" "+" "6.5" "-" "4.3" "*" "4" "/" "5" one by one, I tried to write regular expression for these i.e. "[*]", "[/]","[+]","[-]" and pattern for number, and use:
Scanner sc = new Scanner("3.5+6.5-4.3*4/5");
and use sc.hasNext(pattern);
to loop through each pattern, I expect it to keep matching the start of the string until it match the number pattern, and sc.nect(pattern)
to get "3.5", and then "+" and so on.
But It seems like scanner does not work like this, and after looking through some other classes, I could not find a good solution.
Upvotes: 0
Views: 65
Reputation: 4588
For non-trivial parsing, I advise use of a parser creator (such as JavaCC).
Alternatively, the example you listed might be covered using one of the many scripting languages supported by Java (including JavaScript). See here.
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
// Result is object of type "Double"
Object result = engine.eval("3.5+6.5-4.3*4/5");
// Format output result
System.out.println(new DecimalFormat("0.######").format(result));
... output of this code is
6.56
Upvotes: 1
Reputation: 9601
You can create two capture groups with the following expression:
([0-9\.]+)|([+*\/\-])
This alternates between:
[0-9\.]+
which matches the floating point numbers
and
[+*\/\-]
which matches the mathematical symbols.
List<String> matchList = new ArrayList<String>();
try {
Pattern regex = Pattern.compile("([0-9.]+)|([+*/\\-])");
Matcher regexMatcher = regex.matcher(subjectString);
while (regexMatcher.find()) {
matchList.add(regexMatcher.group());
}
} catch (PatternSyntaxException ex) {
// Syntax error in the regular expression
}
Upvotes: 4