2

I have got a string, say, "5+30" and I need to parse it to tokens 5,30,+. How do I do that? Instead of "+" there can be any arithmetic operator? I think regular expressions will do but I am not a dab at them. Thank you so much for your help.

Anton K.
  • 933
  • 3
  • 9
  • 22
  • Will your expressions have parenthesis? – aioobe Jan 11 '11 at 17:44
  • http://stackoverflow.com/questions/1320891/java-rpn-reverse-polish-notation-infix-to-postfix and http://stackoverflow.com/questions/4240595/getting-wrong-outputs-in-infix-to-postfix-application-with-java seem to cover infix-to-postfix conversion in Java. – dkarp Jan 11 '11 at 17:49
  • No, they will not. Just a simple calculator. – Anton K. Jan 11 '11 at 18:16

5 Answers5

2

I assume you're trying to write some kind of arithmetic processor, so I'll suggest you use a parser generator like ANTLR. It has a tutorial on writing an arithmetic grammar.

Parser generators are very general so it might be overkill for your project, but it's still worth a look. It's always going to be useful to know how (and, more importantly, when) to use a parser generator.

Cameron Skinner
  • 51,692
  • 2
  • 65
  • 86
0

Typically you would use a scanner. Here is an example of a scanner written in java : http://download.oracle.com/javase/tutorial/essential/io/scanning.html

You can also use java.util.Scanner -- here is tutorial : http://www.java-tips.org/java-se-tips/java.util/scanning-text-with-java.util.scanner-3.html

Hogan
  • 69,564
  • 10
  • 76
  • 117
0

A regex that should work for you is:

[0-9]+|[^0-9]+

You would use the java String match or Matcher classes to do the work for you

You can replace the [^0-9]+ part with the set of operators you want to support, for example:

[0-9]+|[+-/*]
kvista
  • 5,039
  • 1
  • 23
  • 25
0

You can look at pattern for good documentation. You String will correspond to something like [0-9]+\p{Punct}{1}[0-9]+ There is examples at the begenning of the doc on how to use it

oyo
  • 1,906
  • 2
  • 16
  • 22
0

Here is an example using the Scanner class:

Scanner s = new Scanner("53+12-1+12");
String token;
while (null != (token = s.findInLine("\\d+|[+-]")))
    System.out.println(token);

Output: (ideone.com demo)

53
+
12
-
1
+
12

Note however, that if you're trying to evaluate the expression this will be of limited help as you will still have to take care of operator precedence and possible parenthisation. I would recommend you to use a proper parser generator for such task.

aioobe
  • 413,195
  • 112
  • 811
  • 826
  • Another solution to the same problem using Scanner is at http://stackoverflow.com/questions/3350312/reading-char-array-of-number-more-than-10/3351024#3351024 – dkarp Jan 11 '11 at 17:54