Java:String.contains 匹配精确的单词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25417363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: String.contains matches exact word
提问by VeilEclipse
In Java
在 Java 中
String term = "search engines"
String subterm_1 = "engine"
String subterm_2 = "engines"
If I do term.contains(subterm_1)
it returns true
. I don't want that. I want the subterm
to exactly match one of the words in term
如果我这样做,term.contains(subterm_1)
它会返回true
. 我不想要那个。我希望subterm
完全匹配中的一个词term
Therefore something like term.contains(subterm_1)
returns false
and term.contains(subterm_2)
returns true
因此,类似term.contains(subterm_1)
退货false
和term.contains(subterm_2)
退货之类的东西true
采纳答案by Jaskey
\b Matches a word boundary where a word character is [a-zA-Z0-9_].
\b 匹配单词字符为 [a-zA-Z0-9_] 的单词边界。
This should work for you, and you could easily reuse this method.
这应该对您有用,您可以轻松地重用此方法。
public class testMatcher {
public static void main(String[] args){
String source1="search engines";
String source2="search engine";
String subterm_1 = "engines";
String subterm_2 = "engine";
System.out.println(isContain(source1,subterm_1));
System.out.println(isContain(source2,subterm_1));
System.out.println(isContain(source1,subterm_2));
System.out.println(isContain(source2,subterm_2));
}
private static boolean isContain(String source, String subItem){
String pattern = "\b"+subItem+"\b";
Pattern p=Pattern.compile(pattern);
Matcher m=p.matcher(source);
return m.find();
}
}
Output:
输出:
true
false
false
true
回答by Scary Wombat
Use indexOf instead and then check whether char at the poistion
使用 indexOf 代替,然后检查是否在位置处的字符
index + length of string plus +1 == ` ` or EOS
or I am sure there is a regex way as well.
或者我确定也有正则表达式。
回答by NV Bhargava
If the words are always separated by spaces, this is one way to go:
如果单词总是用空格分隔,这是一种方法:
String string = "search engines";
String[] parts = string.split(" ");
for(int i = 0; i < parts.length; i++) {
if(parts[i].equals("engine")) {
//do whatever you want
}
回答by Elliott Frisch
I want the subterm to exactly match one of the words in term
我希望 subterm 与 term 中的单词之一完全匹配
Then you can't use contains()
. You could split the term into words and check equality (with or without case sensitivity).
那你就不能用了contains()
。您可以将术语拆分为单词并检查相等性(区分大小写或不区分大小写)。
boolean hasTerm = false;
for (String word : term.split("\s+") {
if (word.equals("engine")) {
hasTerm = true;
break;
}
}
回答by Jhon Kennedy
Since the contains method verify if does exist that array of char in the string, it will aways return true, you will have to use Regex to make this validation.
由于 contains 方法验证字符串中是否确实存在该字符数组,因此它将返回 true,您将不得不使用 Regex 进行此验证。
If the words are aways separed by space it is easier, you can use the \s regex to get it.
如果单词由空格分隔,则更容易,您可以使用 \s 正则表达式来获取它。
Here is a good tutorial: http://www.vogella.com/tutorials/JavaRegularExpressions/article.html
这是一个很好的教程:http: //www.vogella.com/tutorials/JavaRegularExpressions/article.html
回答by shree.pat18
One approach could be to split the string by spaces, convert it to a list, and then use the contains
method to check for exact matches, like so:
一种方法是用空格分割字符串,将其转换为列表,然后使用该contains
方法检查精确匹配,如下所示:
String[] results = term.split("\s+");
Boolean matchFound = Arrays.asList(results).contains(subterm_1);
回答by Jeff C.
I would suggest using word boundaries. If you compile a pattern like \bengines\b, your regular expression will only match on complete words.
我建议使用单词边界。如果你编译一个像\bengines\b 这样的模式,你的正则表达式只会匹配完整的单词。
Here is an explanation of word boundaries, as well as some examples. http://www.regular-expressions.info/wordboundaries.html
这是对单词边界的解释,以及一些示例。 http://www.regular-expressions.info/wordboundaries.html
Also, here is the java API for the pattern, which does include word boundaries http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
此外,这里是模式的 Java API,它确实包括字边界 http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
Here is an example using your requirements above
这是使用上述要求的示例
Pattern p = Pattern.compile("\bengines\b");
Matcher m = p.matcher("search engines");
System.out.println("matches: " + m.find());
p = Pattern.compile("\bengine\b");
m = p.matcher("search engines");
System.out.println("matches: " + m.find());
and here is the output:
这是输出:
matches: true
matches: false