Java regex for matching keywords
Try this.
public static void main(String[] args) {
String largeString = "When I was little I had an Aston-Martin car door slammed shut on my hand and I still remember it quite vividly.";
List<String> keywords = List.of("was", "had", "Martin", "shut", "hen", "ttle", "ar");
Pattern pat = Pattern.compile(keywords.stream()
.collect(Collectors.joining("|", "\S*(", ")\S*")), Pattern.CASE_INSENSITIVE);
Matcher m = pat.matcher(largeString);
while (m.find())
System.out.println(m.group());
}
output:
When
was
little
had
Aston-Martin
car
shut
Read more here: Source link