fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.regex.*;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. public static void main (String[] args) throws java.lang.Exception
  12. {
  13. int i = 0;
  14. String[] toParse = {",1", ".1", "1", "10", "10,5", "333,5", "5,555", "10.50", "blabla 10.5 blabla"};
  15. Pattern pattern = Pattern.compile("(?<![\\d,.])(\\d{1,2})(?:[.,](\\d{1,2}))?(?![\\d,.])");
  16.  
  17. for (String str : toParse) {
  18. Matcher matcher = pattern.matcher(str);
  19. if (matcher.find()) {
  20. String ret = null;
  21. if (matcher.group(2) == null || matcher.group(2).isEmpty()) {
  22. ret = matcher.group(1);
  23. } else if (matcher.group(1) != null) {
  24. ret = matcher.group(1) + "," + matcher.group(2);
  25. }
  26. System.out.println(i + ": " + ret);
  27. }
  28. i++;
  29. }
  30. }
  31. }
Success #stdin #stdout 0.1s 320320KB
stdin
Standard input is empty
stdout
2: 1
3: 10
4: 10,5
7: 10,50
8: 10,5