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

huangapple go评论80阅读模式
英文:

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

问题

我需要检查字符串列表中的任何字符串是否与输入字符串完全匹配(整词搜索),即它不应与字符之间的单词匹配。
例如,请检查下面的代码:

 String input ="我希望数字";
 String [] valid =新的String [] {"蘋果","釘書機" };
 if(Arrays.stream(valid).anyMatch(input :: contains)){
     System.out.println("有效");
 }

我的输出是“有效”,这是不正确的。它从“希望”单词中获取了“蘋果”字符串。我只能在“蘋果”单词是单独的情况下匹配。

英文:

I need to check if any string from a string list matches wholly (whole word search) within the input string i.e. it should not match the word in between characters.
e.g. 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. It is fetching the pin string from the hoping word. I should be able to match only if the pin word is separate.

答案1

得分: 0

请查看以下翻译内容:

按照以下步骤进行操作

    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");
    		}
    	}
    }

注意,`\b` 用于[词边界][1]我在匹配的单词之前和之后添加了它们以创建单词边界


**更多测试**

    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");
    			}
    		}
    	}
    }

**输出**

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

**不使用 [`Stream`][2] 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");
    			}
    		}
    	}
    }


  [1]: https://docs.oracle.com/javase/tutorial/essential/regex/bounds.html
  [2]: https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html
英文:

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.

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:

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");
}
}
}
}

huangapple
  • 本文由 发表于 2020年10月3日 21:31:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/64184827.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定