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. // With the one below, all tests pass with TRUE
  11. // public static String regex = "[\\p{L}\\p{M}\\p{N}\\p{P}\\p{Z}\\p{S}\\p{C}]*" ;
  12. public static String regex = "[\\p{L}\\p{M}\\p{N}\\p{P}\\p{Z}\\p{S}\\p{C}]" ;
  13. public static void main (String[] args) throws java.lang.Exception
  14. {
  15. String userId = "";
  16. testUserId(userId); // false - Correct as we test an empty string with an at-least-one-char regex
  17. userId = "æÆbBcCćĆčČçďĎdzDzdzsDzs";
  18. testUserId(userId); // false - Correct as we only match 1 character string, others fail
  19. userId = "test123";
  20. testUserId(userId); // false - see above
  21. userId = "abcxyzsd";
  22. testUserId(userId); // false - see above
  23.  
  24. String zip = "i«♣│axy";
  25. testZip(zip); // true - OK, 7-symbol string matches against [...]{0,10} regex
  26. zip = "331fsdfsdfasdfasd02c3";
  27. testZip(zip); // false - OK, 21-symbol string does not match a regex that requires only 0 to 10 characters
  28. zip = "331";
  29. testZip(zip); // true - OK, 3-symbol string matches against [...]{0,10} regex
  30.  
  31. }
  32. public static void testUserId(String userId){
  33. boolean pass = true;
  34. if ( !stringValidator(userId, regex)) {
  35. pass = false;
  36. }
  37. System.out.println(pass);
  38. }
  39. public static void testZip(String zip){
  40. boolean pass = true;
  41. String regex1 = regex + "{0,10}";
  42. if (!zip.matches("\\s*") && !stringValidator(zip, regex1)) {
  43. pass = false;
  44. }
  45. System.out.println(pass);
  46. }
  47.  
  48. public static boolean stringValidator(String str, String regex) {
  49. Pattern pattern = Pattern.compile(regex);
  50. Matcher matcher = pattern.matcher(str);
  51. return matcher.matches();
  52. }
  53.  
  54. }
Success #stdin #stdout 0.11s 320512KB
stdin
Standard input is empty
stdout
false
false
false
false
true
false
true