fork download
  1. import java.util.regex.Pattern;
  2.  
  3. public class Main {
  4.  
  5. private static void testar(boolean resultado, String teste) {
  6. System.out.println(Validador.validarCaminho(teste) == resultado ? "Ok" : "ERRO");
  7. }
  8.  
  9. public static void main(String[] args) {
  10. testar(true, "C:/home/PauloNeto/NetBeansProjects/Expre/src/expre/texto.txt");
  11. testar(true, "C:/home/Paulo Neto/Net Beans Projects/Expre/src/expre/texto.txt");
  12. testar(true, "/home/PauloNeto/NetBeansProjects/Expre/src/expre/texto.txt");
  13. testar(true, "/home/Paulo Neto/Net Beans Projects/Expre/src/expre/texto.txt");
  14. testar(true, "home/PauloNeto/NetBeansProjects/Expre/src/expre/texto.txt");
  15. testar(true, "home/Paulo Neto/Net Beans Projects/Expre/src/expre/texto.txt");
  16. testar(true, "home/PauloNeto/NetBeansProjects/Expre/src/expre/texto");
  17. testar(true, "home/Paulo Neto/Net Beans Projects/Expre/src/expre/texto");
  18. testar(true, "home");
  19. testar(true, "/home");
  20. testar(true, "C:/home");
  21. testar(true, "home.txt");
  22. testar(true, "/home.txt");
  23. testar(true, "C:/home.txt");
  24. testar(true, "C:");
  25. testar(true, "C:/");
  26. testar(false, "a:");
  27. testar(false, "a:/");
  28. testar(false, " home");
  29. testar(false, "home ");
  30. testar(false, "home/");
  31. testar(false, "home.");
  32. testar(false, ".txt");
  33. testar(false, "C:home");
  34. testar(false, "C:home/texto");
  35. testar(false, "home//texto.txt");
  36. testar(false, "ho me/PauloNeto/NetBeansProjects/Expre/src/expre/texto.txt");
  37. testar(false, "home/PauloNeto/NetBeansProjects/Expre/src/expre/texto.");
  38. testar(false, "home/PauloNeto/NetBeans#Projects/Expre/src/expre/texto.txt");
  39. testar(false, "home/PauloNeto/NetBeans Projects/Expre/src/expre/texto.txt");
  40. testar(false, "home/PauloNeto/NetBeansProjects/Expre/src/expre/texto..txt");
  41. testar(false, "home/PauloNeto/NetBeansProjects/Expre/src/expre/texto.x.txt");
  42. testar(false, "home/PauloNeto/NetBeansProjects/Expre/src/expre/texto.");
  43. testar(false, "home/PauloNeto/NetBeansProjects/Expre/src/expre/texto.txt.");
  44. testar(false, " E:/home/PauloNeto/NetBeansProjects/Expre/src/expre/texto.txt");
  45. testar(false, "E :/home/PauloNeto/NetBeansProjects/Expre/src/expre/texto.txt");
  46. testar(false, "E: /home/PauloNeto/NetBeansProjects/Expre/src/expre/texto.txt");
  47. testar(false, "E:/ home/PauloNeto/NetBeansProjects/Expre/src/expre/texto.txt");
  48. testar(false, "E:/home /PauloNeto/NetBeansProjects/Expre/src/expre/texto.txt");
  49. testar(false, "home /PauloNeto/NetBeansProjects/Expre/src/expre/texto.txt");
  50. testar(false, " home/PauloNeto/NetBeansProjects/Expre/src/expre/texto.txt");
  51. testar(false, "home/ PauloNeto/NetBeansProjects/Expre/src/expre/texto.txt");
  52. testar(false, "home/PauloNeto /NetBeansProjects/Expre/src/expre/texto.txt");
  53. testar(false, "home/PauloNeto/NetBeansProjects/Expre/src/expre/texto. txt");
  54. testar(false, "home/PauloNeto/NetBeansProjects/Expre/src/expre/texto.txt ");
  55. testar(false, "home/PauloNeto/NetBeansProjects/Expre/src/expre/texto.t xt");
  56. testar(false, "E: ");
  57. testar(false, "E :");
  58. testar(false, " E:");
  59. testar(false, "E:/ ");
  60. testar(false, "E: /");
  61. testar(false, "E :/");
  62. testar(false, " E:/");
  63. testar(false, "");
  64. testar(false, " ");
  65. testar(false, "/");
  66. testar(false, ".");
  67. testar(false, ":");
  68. }
  69. }
  70.  
  71. class Validador {
  72.  
  73. private static final Pattern p1 = Pattern.compile("^[A-Z0-9a-z]*$");
  74.  
  75. private static final Pattern p2 =
  76. Pattern.compile("^(?:(?:[A-Z]\\:)?\\/)?(?:[a-zA-Z0-9]+(?: [a-zA-Z0-9]+)*)(?:\\/(?:[a-zA-Z0-9]+(?: [a-zA-Z0-9]+)*))*(?:\\.[a-zA-Z0-9]+)?|[A-Z]\\:\\/?$");
  77.  
  78. public static boolean validarPalavra(String palavra) {
  79. return p1.matcher(palavra).matches();
  80. }
  81.  
  82. public static boolean validarCaminho(String caminho) {
  83. return p2.matcher(caminho).matches();
  84. }
  85. }
Success #stdin #stdout 0.12s 320512KB
stdin
Standard input is empty
stdout
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok
Ok