Search code examples
regexscalasplittokenizecalculation

Scala: Tokenizing simple arithmetic expressions


How can I split 23+3*5 or 2 + 3*5 into a list List("23", "+", "3", "*", "5")?.

I tried things like split, splitAt, but nothing with the wished result.

I want that it splits at the arithmetic operators.


Solution

  • Try something like

    "2 + 4 - 3 * 5 / 7 / 3".split("(?=[+/*-])|(?<=[+/*-])").map(_.trim)
    

    In this particular example, it gives you:

    Array(2, +, 4, -, 3, *, 5, /, 7, /, 3)
    

    The (?= ) are lookaheads, (?<= ) are lookbehinds. Essentially, it cuts the string before and after every operator. Note that - in [+/*-] is at the last position: otherwise it's interpreted as a character range (e.g. [a-z]).