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. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. List<String> strs = Arrays.asList("D99","M00.0","M01.6","J98.3","T05.0","M96.81","D68.20", "9D.0","6G","7H.","M96.811","J234.82","G687.1","GU87.11");
  13. for (String str : strs)
  14. test(str);
  15.  
  16. }
  17. public static void test(final String myString){
  18. final String rule = "[A-Z]\\d{2}\\.?\\d{0,2}";
  19. final Pattern pattern = Pattern.compile(rule);
  20. final Matcher matcher = pattern.matcher(myString);
  21.  
  22. if(!matcher.matches()){
  23. System.out.println("Failure, the String" + myString + " is not valid!");
  24. } else {
  25. System.out.println("Success, the String" + myString + " is valid!");
  26. }
  27. }
  28. }
Success #stdin #stdout 0.05s 4386816KB
stdin
Standard input is empty
stdout
Success, the StringD99 is valid!
Success, the StringM00.0 is valid!
Success, the StringM01.6 is valid!
Success, the StringJ98.3 is valid!
Success, the StringT05.0 is valid!
Success, the StringM96.81 is valid!
Success, the StringD68.20 is valid!
Failure, the String9D.0 is not valid!
Failure, the String6G is not valid!
Failure, the String7H. is not valid!
Failure, the StringM96.811 is not valid!
Failure, the StringJ234.82 is not valid!
Failure, the StringG687.1 is not valid!
Failure, the StringGU87.11 is not valid!