fork download
  1. import java.util.regex.*;;
  2.  
  3. /* Name of the class has to be "Main" only if the class is public. */
  4. class Ideone
  5. {
  6. private static Pattern PATTERN =
  7. Pattern.compile("^(\\d|[12]\\d|3[01])$");
  8.  
  9. public static void main (String[] args) throws java.lang.Exception
  10. {
  11. String [] numbers = {"0", "03", "5", "10", "31", "32"};
  12. for (String number : numbers) {
  13. Matcher matcher = PATTERN.matcher(number);
  14. if (matcher.matches()) {
  15. System.out.println("Matches for " + number + " : " + matcher.group(1) );
  16. }
  17. else {
  18. System.out.println("No matches for " + number);
  19. }
  20. }
  21. }
  22.  
  23. }
Success #stdin #stdout 0.1s 320512KB
stdin
Standard input is empty
stdout
Matches for 0 : 0
No matches for 03
Matches for 5 : 5
Matches for 10 : 10
Matches for 31 : 31
No matches for 32