0

I am trying to split the string below:

"8-7+5^2"

Into the following:

["8", "7", "5", "2"]

So I am trying to remove all mathematical operators. My regex is below:

expression.split("[+-*/^()]")

But I get an error saying that my regex has an illegal character range. Sorry if its a simple mistake because I'm still learning regex at this point.

Henry Zhu
  • 2,488
  • 9
  • 43
  • 87
  • `expression.split("[-+*/^()]")` – Avinash Raj Jul 25 '15 at 18:36
  • @AvinashRaj Can you explain why I have to switch + and - around? – Henry Zhu Jul 25 '15 at 18:37
  • 1
    `-` is the range character. You're saying you want all the characters between `+` and `*`, which is an illegal range. You have to put `-` at the beginning or the end of the character class. – Teepeemm Jul 25 '15 at 18:38
  • refer . http://stackoverflow.com/questions/13525024/how-to-split-a-mathematical-expression-on-operators-as-delimiters-while-keeping – teksan Jul 25 '15 at 18:40
  • @Teepeemm "*You have to put - at the beginning or the end of the character class"* not quite. We need to either escape `-` with ``\`` or place it somewhere where it can't be treated as range indicator, like at start or end of character class, or right after other range (for instance `[a-c-e]` represents characters `a`,`b`,`c`,`-`,`e`). – Pshemo Jul 25 '15 at 19:07

0 Answers0