fork download
  1. import java.util.regex.Pattern;
  2. import java.util.regex.Matcher;
  3.  
  4.  
  5. class Main
  6. {
  7. public static void main (String[] args) throws java.lang.Exception
  8. {
  9. Pattern p = Pattern.compile("(?:\\.[1-9]+|(?=\\.))(\\.?0\\d*)");
  10.  
  11. String[] strs = {"180.570123", "180.570", "180.0123", "180.0", "180123", "180"};
  12.  
  13. for (String s : strs) {
  14. Matcher m = p.matcher(s);
  15. System.out.printf("%-12s: Match: %s%n", s,
  16. m.find() ? m.group(1) : "n/a");
  17. }
  18. }
  19. }
Success #stdin #stdout 0.11s 212864KB
stdin
Standard input is empty
stdout
180.570123  : Match: 0123
180.570     : Match: 0
180.0123    : Match: .0123
180.0       : Match: .0
180123      : Match: n/a
180         : Match: n/a