Reputation: 963
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.
Upvotes: 2
Views: 575
Reputation: 420921
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.
Upvotes: 0
Reputation: 1924
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
Upvotes: 0
Reputation: 54276
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.
Upvotes: 2
Reputation: 5059
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]+|[+-/*]
Upvotes: 0
Reputation: 70513
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
Upvotes: 0