fork download
  1. import java.util.Arrays;
  2. import java.util.regex.Pattern;
  3.  
  4. class Main {
  5. public static void main(String[] args) {
  6. String[] words = { "card", "creditcard", "debitcard" };
  7.  
  8. String[] testInputs = {
  9. "need to discard pin",
  10. "need to 324card pin",
  11. "need to put out your card!",
  12. };
  13. Arrays.stream(testInputs)
  14. .forEach(testInput -> {
  15. System.out.printf("%-40s = %s\n", testInput, findWords(testInput, words));
  16. });
  17.  
  18.  
  19.  
  20. }
  21.  
  22. public static boolean findWords(String inputStr, String[] words) {
  23. return Arrays.stream(words)
  24. .anyMatch(word -> Pattern
  25. .compile("(?<![A-Za-z-])" + Pattern.quote(word) + "(?![A-Za-z-])")
  26. .matcher(inputStr)
  27. .find());
  28. }
  29. }
Success #stdin #stdout 0.12s 51188KB
stdin
Standard input is empty
stdout
need to discard pin                      = false
need to 324card pin                      = true
need to put out your card!               = true