# Advanced Full-Text Matching with Manticore Search's REGEX Operator

Discover how Manticore Search's latest update brings full-text regular expression pattern matching to search queries, elevating the accuracy and complexity of full-text searches in data analytics, academic research, and intellectual property.

### 介绍

搜索引擎中的高级全文匹配对于提高搜索结果的准确性和相关性至关重要。这在专利分析、合同审查、条款识别和商标搜索等领域能够特别关键，这些领域需要搜索功能的精确性。Manticore Search 从简单的 [`REGEX()`](https://manual.manticoresearch.com/Functions/String_functions#REGEX()) 函数发展到自 [6.3.0](/blog/manticore-search-6-3-0/) 版本起可用的全文 REGEX 操作符，是这一领域的重要进展，为查询提供了复杂且细致的模式匹配。

新推出的 [REGEX 操作符](https://manual.manticoresearch.com/Searching/Full_text_matching/Operators#REGEX-operator) 对于复杂搜索场景尤其有益。例如，在数据分析平台中，它可以搜索日志文件或代码库中的特定模式，识别唯一的错误代码或编程结构。在学术研究数据库中，它使研究人员能够找到具有特定引用风格或文献参考模式的出版物。此外，在商标搜索中，REGEX 操作符对于查找精确或相似的商标非常有价值，因为商标文本的多样性和复杂性。

### 在查询中使用 REGEX
要使用 REGEX 操作符，您的表需要配置 `min_infix_len` 或 `min_prefix_len`。如果您使用过子字符串搜索，这可能已经很熟悉。新的 REGEX 操作符类似于高级通配符操作符。例如，`REGEX(/t.?e/)` 匹配以 't' 开头，后跟任意字符，以 'e' 结尾的任何术语。这突显了该操作符处理多样且复杂文本模式的能力，为搜索查询提供了一种更高级的方法。

### 示例

假设我们有这张表：

```sql
create table brands(name text) min_infix_len='2' charset_table='non_cjk, -'

select * from brands;
--------------

+---------------------+------------------------+
| id                  | name                   |
+---------------------+------------------------+
| 1515699435999330620 | SeaCrest Flower        |
| 1515699435999330621 | C-Crest Flour          |
| 1515699435999330622 | CCrest Flower          |
| 1515699435999330623 | Flower SeaCrest        |
| 1515699435999330624 | RightWrite Stationery  |
| 1515699435999330625 | WriteRight Stationery  |
| 1515699435999330626 | SoleSoul Footwear      |
| 1515699435999330627 | SoulSole Footwear      |
| 1515699435999330628 | PeakBeak Aviaries      |
| 1515699435999330629 | BeakPeak Aviaries      |
| 1515699435999330630 | GrateGreat Kitchenware |
| 1515699435999330631 | GreatGrate Kitchenware |
| 1515699435999330632 | Sunnyside Cyder        |
| 1515699435999330633 | SunnyCide Cyder        |
| 1515699435999330634 | ThymeTime Cooking      |
| 1515699435999330635 | TimeThyme Cooking      |
| 1515699435999330636 | KnightNight Security   |
| 1515699435999330637 | NightKnight Security   |
| 1515699435999330638 | PearPair Electronics   |
| 1515699435999330639 | PairPear Electronics   |
+---------------------+------------------------+
20 rows in set (0.00 sec)
```

以前，要查找类似发音的 `SeaCrest Flower`、`C-Crest Flour` 和 `CCrest Flower` 品牌，查询需要非常详尽：

```sql
select * from brands where match('"SeaCrest flower"|"SeaCrest flour"|"CCrest flower"|"CCrest flour"|"C-Crest flower"|"C-Crest flour"');
```

使用新的 REGEX 操作符，我们可以将此查询简化为：
```sql
select * from brands where match('"REGEX(/(c|sea).*crest/) REGEX(/flo(we|u)r/)"')
--------------

+---------------------+-----------------+
| id                  | name            |
+---------------------+-----------------+
| 1515699435999330620 | SeaCrest Flower |
| 1515699435999330621 | C-Crest Flour   |
| 1515699435999330622 | CCrest Flower   |
+---------------------+-----------------+
3 rows in set (0.00 sec)
```

### RE2 语法和性能
Manticore Search 中的 [REGEX 操作符](https://manual.manticoresearch.com/Searching/Full_text_matching/Operators#REGEX-operator) 遵循 [RE2 语法](https://github.com/google/re2/wiki/Syntax)，以其在处理正则表达式时的高性能和安全性而闻名。由 Google 开发的 RE2 避免了正则表达式处理中常见的陷阱，如灾难性回溯，使其在各种应用中高效且安全。尽管功能强大，REGEX 操作符的使用可能会影响搜索时间，尤其是在扫描大型词典时。用户可能需要在搜索功能的深度和查询性能之间找到平衡。在性能关键的情况下，简化 REGEX 模式并使用额外的搜索过滤器可以帮助减少搜索时间，从而在复杂性和效率之间保持平衡。

### 结论
Manticore Search 引入 [REGEX 操作符](https://manual.manticoresearch.com/Searching/Full_text_matching/Operators#REGEX-operator) 标志着全文搜索功能的重要进步。它提高了搜索结果的精确性和相关性，并在数据分析师、学术研究和商标搜索等各个领域为复杂查询处理开辟了新途径。通过利用 REGEX 操作符，用户可以实现更高水平的搜索效率和准确性，使 Manticore Search 在全文搜索引擎领域成为一个更强大和多功能的工具。
