I am working on an java and MySQL based application and i have task to check whether set of adjacent words present in a given string contains a lot of words. For example
The string is
" java is a programing language .java is robust and powerful and it is also platform independent. "
I want to check whether the substrings
"programing language"
"platform independent"and
"robust and powerful"present in the above string. The substring also must match even if more than one white space occur between the words.
You could try something like:
String string = " java is a programing language .java is robust and powerful and it is also platform independent. ";
String subS1 = "programing language";
subS1 = subS1.replace(" ", "\\s+");
Pattern p1 = Pattern.compile(subS1);
Matcher match1 = string.matcher(subS1);
String subS2 = "platform independent";
subS2 = subS2.replace(" ", "\\s+");
Pattern p2 = Pattern.compile(subS2);
Matcher match2 = string.matcher(subS2);
String subS3 = "robust and powerful";
subS3 = subS3.replace(" ", "\\s+");
Pattern p3 = Pattern.compile(subS3);
Matcher match3 = string.matcher(subS3);
if (match1.find() && match2.find() && match3.find()) {
// Whatever you like
}
You should replace all spaces in the substrings with '\s+', so it will also find "programing [loads of whitespaces] language". Then compile the pattern you want to find and match the string and the substring. Repeat for each substring. Lastly, test whether the matchers found anything.
Some Notes