0

I have this regular expression that tokenizes calculator input strings like 12+3.4x5 to 12,+,3.4,x,5

The regular expression used is

\d+\.?\d+|[\+-÷x]

I get an unexpected match with ^ and letters.

enter image description here

Vincent Tang
  • 3,758
  • 6
  • 45
  • 63
  • 1
    Please don't post duplicates of your own question – Mulan Apr 23 '18 at 02:49
  • 1
    I provided an edit to [my original answer](https://stackoverflow.com/a/49546478/633183) that includes support for decimal numbers – Mulan Apr 23 '18 at 02:49
  • I used shuntyard algorithm instead of eval – Vincent Tang Apr 23 '18 at 02:56
  • 1
    The minus sign (`-`) is a special character in a character class (`[]`). It needs to be escaped. `+` is Unicode `\u002B` and `÷` is unicode `\u00F7`, so `+-÷` is the range of **all** characters from `2B` to `F7`, which includes `^` (`\u005E`). – Andreas Apr 23 '18 at 05:10

1 Answers1

0

Regex solution, although there's probably a cleaner way of writing this if someone could point it out?

(\d+\.?\d+|\d+|(\+|-|÷|x))

enter image description here

Vincent Tang
  • 3,758
  • 6
  • 45
  • 63