如何检查模式是否包含 java 中字符串中存在的多个单词
How to check whether a pattern contain multiple words present in a string in java
我正在开发基于 java 和 MySQL 的应用程序,我的任务是检查给定字符串中存在的一组相邻单词是否包含大量单词。
例如
字符串是
" java is a programing language .java is robust and powerful and it is also platform independent. "
我想检查子字符串
"programing language"
"platform independent"
和 "robust and powerful"
是否出现在上面的字符串中。即使单词之间出现多个白色 space,子字符串也必须匹配。
你可以试试这个作为你问题的第一部分,如果我没有错过理解你的问题,我不会。
String str = " java is a programing language .java is robust and powerful and it is also platform independent. ";
if (str.contains("programing language")) {
System.out.println("programing language");
}
if (str.contains("platform independent")) {
System.out.println("platform independent");
}
if (str.contains("robust and powerful")) {
System.out.println("robust and powerful");
}
我不知道你这是什么意思:子字符串也必须匹配,即使单词之间出现多个白色space。
您可以尝试类似的方法:
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
}
您应该将子字符串中的所有空格替换为“\s+”,这样它也会找到 "programing [loads of whitespaces] language"。
然后编译要查找的模式并匹配字符串和子字符串。对每个子字符串重复。
最后,测试匹配器是否找到任何东西。
一些笔记
- 没有测试它,但它应该让我知道我应该怎么做。
- 此外,这应该可以回答您非常具体的问题。当您有大量子字符串要检查时,不应不使用此方法...
- 请 post 一些你已经在使用的代码,因为现在我想做你的家庭作业...
我正在开发基于 java 和 MySQL 的应用程序,我的任务是检查给定字符串中存在的一组相邻单词是否包含大量单词。 例如
字符串是
" java is a programing language .java is robust and powerful and it is also platform independent. "
我想检查子字符串
"programing language"
"platform independent"和
"robust and powerful"是否出现在上面的字符串中。即使单词之间出现多个白色 space,子字符串也必须匹配。
你可以试试这个作为你问题的第一部分,如果我没有错过理解你的问题,我不会。
String str = " java is a programing language .java is robust and powerful and it is also platform independent. ";
if (str.contains("programing language")) {
System.out.println("programing language");
}
if (str.contains("platform independent")) {
System.out.println("platform independent");
}
if (str.contains("robust and powerful")) {
System.out.println("robust and powerful");
}
我不知道你这是什么意思:子字符串也必须匹配,即使单词之间出现多个白色space。
您可以尝试类似的方法:
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
}
您应该将子字符串中的所有空格替换为“\s+”,这样它也会找到 "programing [loads of whitespaces] language"。 然后编译要查找的模式并匹配字符串和子字符串。对每个子字符串重复。 最后,测试匹配器是否找到任何东西。
一些笔记
- 没有测试它,但它应该让我知道我应该怎么做。
- 此外,这应该可以回答您非常具体的问题。当您有大量子字符串要检查时,不应不使用此方法...
- 请 post 一些你已经在使用的代码,因为现在我想做你的家庭作业...