2

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.

Andrey Tyukin
  • 43,673
  • 4
  • 57
  • 93
Lockna
  • 691
  • 1
  • 8
  • 24

2 Answers2

3

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]).

Andrey Tyukin
  • 43,673
  • 4
  • 57
  • 93
0

I suggest matching on what you want as tokens. e.g.

"\\d+|[-+*/]".r.findAllIn(" 23 + 4 * 5 / 7").toList
// List(23, +, 4, *, 5, /, 7)
Volty De Qua
  • 199
  • 6