fork download
  1. //'main' method must be in a class 'Rextester'.
  2. //Compiler version 1.8.0_111
  3.  
  4. import java.util.*;
  5. import java.lang.*;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8.  
  9. class Rextester
  10. {
  11. public static void main(String args[])
  12. {
  13. final String regex = "^(?:(0[1-9]|[1-9][0-9])0{6}|(0[1-9]|[1-9][0-9]){2}0{4}|((?:0[1-9]|[1-9][0-9])){3}00|(?:0[1-9]|[1-9][0-9]){4})$";
  14. final String string = "00000000\n"
  15. + "01000000\n"
  16. + "10000000\n"
  17. + "10440044\n"
  18. + "99990000\n"
  19. + "99999900\n"
  20. + "99000099\n"
  21. + "77007788\n"
  22. + "55555555";
  23.  
  24. final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
  25. final Matcher matcher = pattern.matcher(string);
  26.  
  27. while (matcher.find()) {
  28. System.out.println("Full match: " + matcher.group(0));
  29. if (null != matcher.group(1)) {
  30. System.out.println("Level 1");
  31. } else if (null != matcher.group(2)) {
  32. System.out.println("Level 2");
  33. } else if (null != matcher.group(3)) {
  34. System.out.println("Level 3");
  35. } else {
  36. System.out.println("Level 4");
  37. }
  38. }
  39. }
  40. }
Success #stdin #stdout 0.05s 2184192KB
stdin
Standard input is empty
stdout
Full match: 01000000
Level 1
Full match: 10000000
Level 1
Full match: 99990000
Level 2
Full match: 99999900
Level 3
Full match: 55555555
Level 4