亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用正則表達式在文本中搜索多個單詞 (Java)

使用正則表達式在文本中搜索多個單詞 (Java)

慕絲7291255 2023-03-09 15:12:28
我有一種方法可以在文本中搜索單詞,這兩個單詞都是由參數插入的。public Integer findTheWord(String stringToCheck, String regexString) throws IOException {        int count = 0;        Pattern regexp = Pattern.compile("\\b" + regexString + "\\b");        Matcher matcher = regexp.matcher(stringToCheck);        while (matcher.find()) {                count++;                String matchString = matcher.group();                System.out.println(matchString);            }        System.out.println(count);        return count;  }如何插入多個單詞并返回每個單詞的出現?
查看完整描述

2 回答

?
泛舟湖上清波郎朗

TA貢獻1818條經驗 獲得超3個贊

因此,第一個也是最簡單的選擇是使用您的實際findTheWord()方法并創建一個使用它的新方法:


public Map<String, Integer> findTheWords(String stringToCheck, List<String> words) {

    return words.stream().distinct()

            .collect(Collectors.toMap(Function.identity(), word -> findTheWord(stringToCheck, word)));

}


public Integer findTheWord(String stringToCheck, String regexString) {

    Pattern regexp = Pattern.compile("\\b" + regexString + "\\b");

    Matcher matcher = regexp.matcher(stringToCheck);


    int count = 0;

    while (matcher.find()) {

        count++;

    }

    return count;

}

這樣做的問題是,如果您使用大量單詞來查找大文本,因為它會為每個單詞遍歷給定的字符串。因此,另一種方法是為所有單詞創建一個正則表達式,并在生成的映射中遞增下一個找到的單詞:


public Map<String, Integer> findTheWords(String stringToCheck, List<String> words) {

    Pattern regexp = Pattern.compile(words.stream().distinct().map(word -> "\\b" + word + "\\b").collect(Collectors.joining("|")));

    // creates a pattern like this: "\ba\b|\bb\b|\bc\b|\bd\b|\be\b"

    Matcher matcher = regexp.matcher(stringToCheck);

    Map<String, Integer> result = new HashMap<>();

    while (matcher.find()) {

        String word = matcher.group();

        result.put(word, result.getOrDefault(word, 0) + 1);

    }

    return result;

}

除此之外,您可能正在考慮對Set單詞使用 a 而不是 the List,因為值是唯一的,因此無需調用.distinct()流。


查看完整回答
反對 回復 2023-03-09
?
森欄

TA貢獻1810條經驗 獲得超5個贊

HashMap 作為參數,輸入字符串作為鍵,正則表達式作為值,遍歷所有條目,執行你的方法并返回一個 HashMap,匹配的詞作為鍵,出現作為值。


 public HashMap<String, Integer> findTheWordsAndOccurences(HashMap<String, String> stringsAndRegex) throws IOException {


    HashMap<String, Integer> result = null;


    for (Map.Entry<String, String> entry : stringsAndRegex.entrySet()){


        String stringToCheck = entry.getKey();

        String regexString = entry.getValue();

        String matchString = "";

        int count = 0;

        Pattern regexp = Pattern.compile("\\b" + regexString + "\\b");

        Matcher matcher = regexp.matcher(stringToCheck);


        while (matcher.find()) {

            count++;

            matchString = matcher.group();

            System.out.println(matchString);

            result.put(matchString, count);

        }

    }

    return result;

}


查看完整回答
反對 回復 2023-03-09
  • 2 回答
  • 0 關注
  • 273 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號