Match any word from a list / array in a java expression

I have Listwords as below

List<String> forbiddenWordList = Arrays.asList("LATE", "S/O", "SO", "W/O", "WO");

How can I understand StringContains any word of the word List. as....

String name1 = "Adam Smith";      // false (not found)
String name2 = "Late H Milton";   // true  (found Late)
String name3 = "S/O Furi Kerman"; // true  (found S/O)
String name4 = "Conl Faruk";      // false (not found)
String name5 = "Furi Kerman WO";  // true  (found WO)

Regular expression is much appreciated.

+4
source share
5 answers

Finally, I have a solution on my own with the help of all of you ....

    String regex = String.join("|", forbiddenWordList.stream().map(word -> "\\b" + word + "\\b").collect(Collectors.toList()));
    Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
    System.out.println(pattern.matcher(name).find());

Word Boundary ( \\b) helps you find the exact word, not the matched text. Thank you all for your help.

0
source
boolean containsForbiddenName = forbiddenWordList.stream()
     .anyMatch(forbiddenName -> name.toLowerCase()
          .contains(forbiddenName.toLowerCase()));
+8
source
  • |

    Delimited = String.join( "|", forbiddenWordList)

  • WordPattern       = Pattern.compile(listDelimited, Pattern.CASE_INSENSITIVE);

  • boolean hasForbiddenWord = forbiddenWordPattern.matcher().find();

( @Maurice Perry)

+3

:

Iterate over the words ( stream) and returns true if any words (with a name w) match the condition ( contains)

public static boolean isForbidden(String word, List<String> words) {
     return words.stream().anyMatch(w -> (word.toLowerCase().contains(w.toLowerCase())));
}

Using regex , he will build the template itself fromList

public static boolean isForbidden1(String word, List<String> words) {
     String forbiddenWordPattern = String.join("|", words);

     return Pattern.compile(forbiddenWordPattern, Pattern.CASE_INSENSITIVE)
                   .matcher(word)
                   .find();
 }
+2
source

The list can be expressed as a template:

Pattern forbiddenWordPattern
        = Pattern.compile("LATE|S/O|SO|W/O|WO", Pattern.CASE_INSENSITIVE);

To check for a word in a text, you should:

boolean hasForbiddenWord = forbiddenWordPattern.matcher(text).find();
+2
source

Source: https://habr.com/ru/post/1680883/


All Articles