开发者

Parse calculations tokens

I have got a string, say, "5+30" a开发者_开发技巧nd 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.


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.


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.


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


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]+|[+-/*]


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜