0
String[] tokens = infix.split("[0-9]*([.][0-9]+)? | sin | cos | tan | log | \^ | sqrt | \( | \) | \+ | \- | \* | \/");

When I compile this, it says I have an illegal escape character at "\^", I'm trying to tell it to ignore the special character "^" and view "^" as an actual String, don't I have the syntax right?

Jakkie Chan
  • 317
  • 4
  • 14

1 Answers1

3

In java, you need to escape the escapes. Essentially, you must use double backslashes for string literals:

String[] tokens = infix.split("[0-9]*([.][0-9]+)? | sin | cos | tan | log | \\^ | sqrt | \\( | \\) | \\+ | \\- | \\* | /");

Note that you don't escape forward slashes in java (regex or not).

As an aside, you regex may be simplified to:

String[] tokens = infix.split("[0-9]*([.][0-9]+)? | sin | cos | tan | log | sqrt | [()+*-] | /");
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • hmm i tried that, but my calc GUI still crashed when I used it – Jakkie Chan Mar 31 '14 at 00:06
  • @user3427042 I just tested the regex and it compiled (ie executed) OK. I think there is a problem with the regex not related to escaping. I suspect you don't want all those spaces in there. – Bohemian Mar 31 '14 at 00:09
  • "[()+*-]" since those are all special characters, wouldn't I need a "\" before each one to escape its special regex meaning? – Jakkie Chan Mar 31 '14 at 00:09
  • No. Characters in a character class don't need escaping, except for very few special cases. – Bohemian Mar 31 '14 at 00:10
  • oh ok tysm, and so could I include a "^" and "/" in there too? – Jakkie Chan Mar 31 '14 at 00:11
  • Yes, although the carat `^` should not be the first character (otherwise you'll create a *negated* character class). The forward slash is not a special character at any time, nor does it have any regex meaning in any language (some languages use the forward slash to *delimit* a regex, but it's not *part* of the regex - on those languages, you need to escape the slash to express a literal slash). Note that the minus sign must be first or last in a character class, otherwise it defines a character *range*, eg `[a-z]` means any lowercase letter, but `[az-]` means "a, z or dash" – Bohemian Mar 31 '14 at 00:51