Guest User

Untitled

a guest
Dec 14th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. public static void isMatch(String regex, String product)
  2. {
  3. try {
  4. System.out.println("regex(" + regex + ") product(" + product + ")");
  5. Pattern pattern = Pattern.compile(regex.toLowerCase());
  6. Matcher matcher = pattern.matcher(product.toLowerCase());
  7. System.out.println(matcher.matches() ? "truen" : "falsen");
  8. } catch (Exception e) {
  9. System.out.println(e);
  10. }
  11. }
  12.  
  13. public static void main(String[] args)
  14. {
  15. // true ?!
  16. isMatch("a*.*/REGEX", "a1.0/REGEX");
  17. isMatch("/*.*/REGEX", "/1.0/REGEX");
  18. isMatch("/*.*/REGEX", "/abc/REGEX");
  19.  
  20. // Exception as expected
  21. isMatch("*.*/REGEX", "1.0/REGEX");
  22. }
  23.  
  24. ## Why are these cases working?
  25. regex(a*.*/REGEX) product(a1.0/REGEX)
  26. true
  27.  
  28. regex(/*.*/REGEX) product(/1.0/REGEX)
  29. true
  30.  
  31. regex(/*.*/REGEX) product(/abc/REGEX)
  32. true
  33.  
  34. ## This is what I expected to happened
  35. regex(*.*/REGEX) product(1.0/REGEX)
  36. java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0
  37. *.*/regex
  38. ^
Add Comment
Please, Sign In to add comment