搜索算法
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