fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7.  
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. String regex = "([0-9]+(?:\\.[0-9]+)?|[+]|[*]|[0-9]+)|\\S+";
  13. String string = "45 + 31.05 * 110 @ 54";
  14.  
  15. Pattern pattern = Pattern.compile(regex);
  16. Matcher matcher = pattern.matcher(string);
  17.  
  18. while (matcher.find()) {
  19. if (matcher.group(1) == null) {
  20. // your Exception here
  21. //throw new Exception("No match!");
  22. System.out.println(matcher.group() + " no match");
  23. } else {
  24. System.out.println(matcher.group(1) + " match");
  25. }
  26. }
  27. }
  28. }
Success #stdin #stdout 0.12s 50520KB
stdin
Standard input is empty
stdout
45  match
+  match
31.05  match
*  match
110  match
@ no match
54  match