I have an List
of word like below
List<String> forbiddenWordList = Arrays.asList("LATE", "S/O", "SO", "W/O", "WO");
How can I understand a String
Contains any one of the word of the List
. like....
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 highly appreciated.
turn the list to a string with the | delimiter
String listDelimited = String.join("|", forbiddenWordList )
create the regex
Pattern forbiddenWordPattern = Pattern.compile(listDelimited , Pattern.CASE_INSENSITIVE);
test your text
boolean hasForbiddenWord = forbiddenWordPattern.matcher(text).find();
(similar to the answer of @Maurice Perry)
boolean containsForbiddenName = forbiddenWordList.stream()
.anyMatch(forbiddenName -> name.toLowerCase()
.contains(forbiddenName.toLowerCase()));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With