java regex specific contain word
www.igiftidea.com
To check if a string contains a specific word using regular expressions in Java, you can use the word boundary "\b" in your regular expression pattern. The word boundary matches the position between a word character (as defined by the regular expression engine) and a non-word character. Here's an example:
String input = "The quick brown fox jumps over the lazy dog"; String wordToMatch = "fox"; if (input.matches(".*\\b" + wordToMatch + "\\b.*")) { System.out.println("Input contains the word \"" + wordToMatch + "\"."); } else { System.out.println("Input does not contain the word \"" + wordToMatch + "\"."); }
In the example above, the regular expression pattern ".\bfox\b." matches any string that contains the word "fox". The "\b" is used to ensure that only the whole word "fox" is matched and not part of another word that contains "fox". Note that the double backslashes "" are used to escape the backslash character in the regular expression pattern.
You can replace the "fox" string with any word you want to match.