简体   繁体   English

如何检查字符串列表中的特定单词是否包含在字符串中,但不应该在任何其他单词之间?

[英]How to check if a particular word from a string list contains in a string, but it should not be between any other words?

I need to check if any string from a string list matches wholly (whole word search) within the input string ie it should not match the word in between characters.我需要检查字符串列表中的任何字符串是否在输入字符串中完全匹配(整个单词搜索),即它不应该匹配字符之间的单词。 eg check the code below:例如检查下面的代码:

String input = "i was hoping the number";
String[] valid = new String[] { "nip", "pin" };
if (Arrays.stream(valid).anyMatch(input::contains)) {
    System.out.println("valid");
}

My output is valid , which is not correct.我的输出是valid ,这是不正确的。 It is fetching the pin string from the hoping word.它正在从hoping词中获取pin字符串。 I should be able to match only if the pin word is separate.只有当pin词是分开的时,我才应该能够匹配。

Do it as follows:请按以下步骤操作:

import java.util.Arrays;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        String input = "i was hoping the number";
        String[] valid = new String[] { "nip", "pin" };
        if (Arrays.stream(valid).anyMatch(p -> Pattern.compile("\\b" + p + "\\b").matcher(input).find())) {
            System.out.println("valid");
        }
    }
}

Note that \\b is used for word boundary which I have added before and after the matching words to create word boundary for them.请注意, \\b用于词边界,我在匹配词之前和之后添加了它来为它们创建词边界。

Some more tests:还有一些测试:

import java.util.Arrays;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        String[] testStrings = { "i was hoping the number", "my pin is 123", "the word, turnip ends with nip",
                "turnip is a vegetable" };
        String[] valid = new String[] { "nip", "pin" };
        for (String input : testStrings) {
            if (Arrays.stream(valid).anyMatch(p -> Pattern.compile("\\b" + p + "\\b").matcher(input).find())) {
                System.out.println(input + " => " + "valid");
            } else {
                System.out.println(input + " => " + "invalid");
            }
        }
    }
}

Output:输出:

i was hoping the number => invalid
my pin is 123 => valid
the word, turnip ends with nip => valid
turnip is a vegetable => invalid

Solution without using Stream API:不使用Stream API的解决方案:

import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        String input = "i was hoping the number";
        String[] valid = new String[] { "nip", "pin" };
        for (String toBeMatched : valid) {
            if (Pattern.compile("\\b" + toBeMatched + "\\b").matcher(input).find()) {
                System.out.println("valid");
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查字符串是否包含特定单词 - To check if string contains particular word 如果字符串中包含我的String数组中的任何单词,如何替换字符串中的单词 - How to replace words from a String if it contains any word from my String array 检查字符串是否包含列表中的任何字符串 - Check if a string contains any of the strings from an List 如何检查字符串中存在的任何单词列表? - How to check any of the list of words present in the string? 如何检查数组是否包含字符串中的特定单词并获取它? - How to check if an Array contains a particular word in a String and get it? 如何检查列表中的所有项目是否包含字符串中的任何字符 - How to check that all items in the list contains any character from string 检查一个字符串中所有单词是否都存在于另一个字符串中 - Check that word all words from one string exist in the other 使用 Java Regex,如何检查字符串是否包含集合中的任何单词? - Using Java Regex, how to check if a string contains any of the words in a set ? HQL检查字段是否包含字符串列表中的任何字符串 - HQL to check if a field CONTAINS any of the String from a list of String MySQL检查列是否包含来自字符串的单词 - MySQL check if column contains words from String
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM