英文:
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");
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论