搜尋演算法
SearchMatch
是一個用於將文字與模式匹配的介面。匹配結果位於返回值 SearchMatchResult
中。匹配結果包含匹配位置資訊和總匹配分數。
fzf.
實現
FuzzyMatchV2Search
移植自 fzf FuzzyMatchV2Search 演算法。執行快速模糊搜尋,非常適用於快速查詢路徑。
ExactMatchNaive
移植自 fzf ExactMatchNaive 演算法。簡單的精確匹配,如果您知道要搜尋的內容,它會更準確。
SearchMatch
演算法和預設語法隱藏在包保護類中,因為在確定 API 能夠獲得長期支援之前,我們不希望完全公開它們。您需要透過其內建的構建器來構造 SearchMatch
。
SearchMatch searchMatch = SearchMatch.builder()
.caseSensitive(false)
.normalize(false)
.forward(true)
.build();
可以配置區分大小寫、搜尋發生的方向或搜尋前是否應將文字標準化。當不同語言中同類字元有細微變化時,標準化會很有用。
搜尋演算法是根據下表所示的搜尋語法選擇的。
標記 | 匹配型別 | 描述 |
---|---|---|
|
模糊匹配 |
匹配 |
|
精確匹配 |
包含 |
示例
SearchMatch searchMatch = SearchMatch.builder()
.caseSensitive(false)
.normalize(false)
.forward(true)
.build();
SearchMatchResult result = searchMatch.match("foo bar baz", "fbb");
result.getStart();
// 0 - start position inclusive
result.getEnd();
// 9 - end position exclusive
result.getPositions();
// 0,4,8 - positions, inclusive
result.getScore();
// 112 - score
result.getAlgorithm();
// FuzzyMatchV2SearchMatchAlgorithm - resolved algo