質問

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.

役に立ちましたか?

解決

You can create two capture groups with the following expression:

Regex

([0-9\.]+)|([+*\/\-])

Brief explanation

This alternates between:

[0-9\.]+ which matches the floating point numbers

and

[+*\/\-] which matches the mathematical symbols.

Example Java code

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
}

他のヒント

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top