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评论104阅读模式
英文:

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

问题

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

  1. String input ="我希望数字";
  2. String [] valid =新的String [] {"蘋果","釘書機" };
  3. ifArrays.streamvalid).anyMatchinput :: contains)){
  4. System.out.println("有效");
  5. }

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

英文:

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:

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

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

请查看以下翻译内容:

  1. 按照以下步骤进行操作
  2. import java.util.Arrays;
  3. import java.util.regex.Pattern;
  4. public class Main {
  5. public static void main(String[] args) {
  6. String input = "i was hoping the number";
  7. String[] valid = new String[] { "nip", "pin" };
  8. if (Arrays.stream(valid).anyMatch(p -> Pattern.compile("\\b" + p + "\\b").matcher(input).find())) {
  9. System.out.println("valid");
  10. }
  11. }
  12. }
  13. 注意`\b` 用于[词边界][1]我在匹配的单词之前和之后添加了它们以创建单词边界
  14. **更多测试**
  15. import java.util.Arrays;
  16. import java.util.regex.Pattern;
  17. public class Main {
  18. public static void main(String[] args) {
  19. String[] testStrings = { "i was hoping the number", "my pin is 123", "the word, turnip ends with nip",
  20. "turnip is a vegetable" };
  21. String[] valid = new String[] { "nip", "pin" };
  22. for (String input : testStrings) {
  23. if (Arrays.stream(valid).anyMatch(p -> Pattern.compile("\\b" + p + "\\b").matcher(input).find())) {
  24. System.out.println(input + " => " + "valid");
  25. } else {
  26. System.out.println(input + " => " + "invalid");
  27. }
  28. }
  29. }
  30. }
  31. **输出**
  32. i was hoping the number => invalid
  33. my pin is 123 => valid
  34. the word, turnip ends with nip => valid
  35. turnip is a vegetable => invalid
  36. **不使用 [`Stream`][2] API 的解决方案**
  37. import java.util.regex.Pattern;
  38. public class Main {
  39. public static void main(String[] args) {
  40. String input = "i was hoping the number";
  41. String[] valid = new String[] { "nip", "pin" };
  42. for (String toBeMatched : valid) {
  43. if (Pattern.compile("\\b" + toBeMatched + "\\b").matcher(input).find()) {
  44. System.out.println("valid");
  45. }
  46. }
  47. }
  48. }
  49. [1]: https://docs.oracle.com/javase/tutorial/essential/regex/bounds.html
  50. [2]: https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html
英文:

Do it as follows:

  1. import java.util.Arrays;
  2. import java.util.regex.Pattern;
  3. public class Main {
  4. public static void main(String[] args) {
  5. String input = "i was hoping the number";
  6. String[] valid = new String[] { "nip", "pin" };
  7. if (Arrays.stream(valid).anyMatch(p -> Pattern.compile("\\b" + p + "\\b").matcher(input).find())) {
  8. System.out.println("valid");
  9. }
  10. }
  11. }

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:

  1. import java.util.Arrays;
  2. import java.util.regex.Pattern;
  3. public class Main {
  4. public static void main(String[] args) {
  5. String[] testStrings = { "i was hoping the number", "my pin is 123", "the word, turnip ends with nip",
  6. "turnip is a vegetable" };
  7. String[] valid = new String[] { "nip", "pin" };
  8. for (String input : testStrings) {
  9. if (Arrays.stream(valid).anyMatch(p -> Pattern.compile("\\b" + p + "\\b").matcher(input).find())) {
  10. System.out.println(input + " => " + "valid");
  11. } else {
  12. System.out.println(input + " => " + "invalid");
  13. }
  14. }
  15. }
  16. }

Output:

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

Solution without using Stream API:

  1. import java.util.regex.Pattern;
  2. public class Main {
  3. public static void main(String[] args) {
  4. String input = "i was hoping the number";
  5. String[] valid = new String[] { "nip", "pin" };
  6. for (String toBeMatched : valid) {
  7. if (Pattern.compile("\\b" + toBeMatched + "\\b").matcher(input).find()) {
  8. System.out.println("valid");
  9. }
  10. }
  11. }
  12. }

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:

确定