Question
How can I use Java Regex to check if a string contains any specific words from a defined set?
Answer
In Java, you can use the built-in regular expressions (Regex) to efficiently determine if a string contains any words from a specified set. This can be particularly useful in scenarios like content moderation, input validation, or where certain triggers need to be identified.
import java.util.regex.*;
public class RegexExample {
public static void main(String[] args) {
String input = "This is a sample string containing some keywords.";
String[] wordsToSearch = {"sample", "keywords", "test"};
String regex = String.join("|", wordsToSearch);
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
boolean found = matcher.find();
if (found) {
System.out.println("The string contains at least one of the words from the set.");
} else {
System.out.println("No matches found.");
}
}
}
Causes
- The need to search for specific keywords within a larger body of text.
- Filtering content based on certain words for user-generated input.
Solutions
- Define a set of words that you want to search for.
- Use a regular expression pattern that matches any of these words.
- Utilize the `Pattern` and `Matcher` classes from the `java.util.regex` package to perform the search.
Common Mistakes
Mistake: Forgetting to escape special characters in regex words.
Solution: Use `Pattern.quote(word)` to escape any special characters.
Mistake: Using incorrect regex syntax which leads to unexpected results.
Solution: Double-check the regex pattern or test it using regex tester tools.
Helpers
- Java Regex
- check string words set
- Java regular expressions
- Pattern and Matcher Java
- find words in string using regex