Java中有没有一种有效的方法来搜索同一个字符串值中的多个单词(不是彼此相邻的)?

问题描述 投票:0回答:3

我们将大量数据从权威来源提取到我们环境中的各种属性中。

我们拥有的属性之一是“jobTitle” - 顾名思义,它是一个身份在组织中各自的职位名称。

我的主要工作之一是为组织中的角色创建分配规则,我遇到了一个我认为可以更有效地完成的问题,但我缺少 Java 知识

所以问题来了:

我们使用以下 Java 行来“获取”我们组织中包含护士的任何特定职位:

identity.getAttribute("jobTitle").contains("Nurse");

我向 Java 专家提出的问题 - 有没有一种方法可以利用通配符来提取包含 %Nursing%Specialist% 或 %Nursing%Coordinator% 的所有职位..

因此,如果我想向所有具有“护理专业发展专家”或“护理资源协调员”职位的用户提供该角色,例如,单词协调员专家

可能会被分隔其他字符串。

Java 有没有一种有效的方法来克服这个挑战?

例如-

当前如果以下匹配则返回 true:
return "Nursing Care Coordinator".equalsIgnoreCase(identity.getAttribute("jobTitle")) || "Nursing Resource Coordinator".equalsIgnoreCase(identity.getAttribute("jobTitle")) || "Nursing Practice Specialist".equalsIgnoreCase(identity.getAttribute("jobTitle")) ||  "Nursing Professional Development Specialist".equalsIgnoreCase(identity.getAttribute("jobTitle"));

%Nursing%Specialist% - 这些 % 可能是其他字符串,例如 Professional Nursing Development Specialist ,因此只是想确保所有必要的职位名称都获得正确的角色。希望不必能够输入每个特定的职位名称!!
java database roles contains identity
3个回答
1
投票

可以使用正则表达式来处理搜索字符串中不一定相邻的多个单词。

您可以使用 java.util.regex 中的 Java 'Pattern' 和 'Matcher' 类来完成此操作。您可以使用 regexr

来帮助您构建和测试正则表达式。
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class MultiWordSearch {
    public static boolean containsWords(String input, String[] words) {
        // Building a regex pattern like "(?=.*word1)(?=.*word2)(?=.*word3).*"
        StringBuilder regex = new StringBuilder();
        for (String word : words) {
            regex.append("(?=.*").append(Pattern.quote(word)).append(")");
        }
        regex.append(".*");

        Pattern pattern = Pattern.compile(regex.toString(), Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(input);

        return matcher.matches();
    }

    public static void main(String[] args) {
        String input = "This is a sample string to search";
        String[] words = {"sample", "search"};

        System.out.println("Contains words: " + containsWords(input, words));
    }
}

0
投票

正如 @Dave Newton 所指出的,“高效”一词的解释非常广泛。

一种方法是简单地计算给定 jobTitle 中有多少“关键字”,返回该计数并对其执行操作。比如:
public static boolean containsMultipleKeywords(String jobTitle) {
  // for case-insensitive comparison
  jobTitle = jobTitle.toLowerCase();

  // Set of keywords to check
  Set<String> keywords = new HashSet<>(Arrays.asList("nurse", "coordinator", "director"));

  // Count the occurrences of keywords
  int count = 0;
  for (String keyword : keywords) {
    if (jobTitle.contains(keyword)) {
      count++;
    }
  }

  // Return true if at least two keywords are found
  return count >= 2;
}

您也可以使用正则表达式:
  Pattern pattern = Pattern.compile("\\b(nurse|coordinator|director)\\b");

  // Matcher to find matches in the jobTitle
  Matcher matcher = pattern.matcher(jobTitle);

  // Count the number of matches
  int count = 0;
  while (matcher.find()) {
    count++;
  }

0
投票

您可以使用正则表达式为程序员高效地完成此操作(一个表达式):
identity.getAttribute("jobTitle")
  .matches("^(?=.*Nurse.*(Coordinator|Specialist)).*")

对于机器来说效率相当高。

在英语中,正则表达式意味着 输入中的某个位置存在“护士”,之后的某个位置存在“协调员”或“专家”。

如果您只是想断言术语以任意顺序出现,则可以使用两次前瞻 - 每个术语一个:

matches("^(?=.*Nurse)(?=.*(Coordinator|Specialist)).*")
© www.soinside.com 2019 - 2024. All rights reserved.