re2
使用 RE2 的 ClickHouse 兼容正则函数
概览
| 扩展包名 | 版本 | 分类 | 许可证 | 语言 |
|---|---|---|---|---|
re2 | 0.3.0 | UTIL | PostgreSQL | C++ |
| ID | 扩展名 | Bin | Lib | Load | Create | Trust | Reloc | 模式 |
|---|---|---|---|---|---|---|---|---|
| 4235 | re2 | 否 | 是 | 否 | 是 | 是 | 是 | - |
release 0.3.0; SQL v0.3
版本
| 类型 | 仓库 | 版本 | PG 大版本 | 包名 | 依赖 |
|---|---|---|---|---|---|
| EXT | PIGSTY | 0.3.0 | 1817161514 | re2 | - |
| RPM | PIGSTY | 0.3.0 | 1817161514 | re2_$v | - |
| DEB | PIGSTY | 0.3.0 | 1817161514 | postgresql-$v-re2 | - |
| OS / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
| el8.x86_64 | PIGSTY 0.3.0 el8.x86_64.pg18 : re2_18 re2_18-0.3.0-1PIGSTY.el8.x86_64.rpm
| PIGSTY 0.3.0 el8.x86_64.pg17 : re2_17 re2_17-0.3.0-1PIGSTY.el8.x86_64.rpm
| PIGSTY 0.3.0 el8.x86_64.pg16 : re2_16 re2_16-0.3.0-1PIGSTY.el8.x86_64.rpm
| PIGSTY MISS | PIGSTY MISS |
| el8.aarch64 | PIGSTY 0.3.0 el8.aarch64.pg18 : re2_18 re2_18-0.3.0-1PIGSTY.el8.aarch64.rpm
| PIGSTY 0.3.0 el8.aarch64.pg17 : re2_17 re2_17-0.3.0-1PIGSTY.el8.aarch64.rpm
| PIGSTY 0.3.0 el8.aarch64.pg16 : re2_16 re2_16-0.3.0-1PIGSTY.el8.aarch64.rpm
| PIGSTY MISS | PIGSTY MISS |
| el9.x86_64 | PIGSTY 0.3.0 el9.x86_64.pg18 : re2_18 re2_18-0.3.0-1PIGSTY.el9.x86_64.rpm
| PIGSTY 0.3.0 el9.x86_64.pg17 : re2_17 re2_17-0.3.0-1PIGSTY.el9.x86_64.rpm
| PIGSTY 0.3.0 el9.x86_64.pg16 : re2_16 re2_16-0.3.0-1PIGSTY.el9.x86_64.rpm
| PIGSTY MISS | PIGSTY MISS |
| el9.aarch64 | PIGSTY 0.3.0 el9.aarch64.pg18 : re2_18 re2_18-0.3.0-1PIGSTY.el9.aarch64.rpm
| PIGSTY 0.3.0 el9.aarch64.pg17 : re2_17 re2_17-0.3.0-1PIGSTY.el9.aarch64.rpm
| PIGSTY 0.3.0 el9.aarch64.pg16 : re2_16 re2_16-0.3.0-1PIGSTY.el9.aarch64.rpm
| PIGSTY MISS | PIGSTY MISS |
| el10.x86_64 | PIGSTY 0.3.0 el10.x86_64.pg18 : re2_18 re2_18-0.3.0-1PIGSTY.el10.x86_64.rpm
| PIGSTY 0.3.0 el10.x86_64.pg17 : re2_17 re2_17-0.3.0-1PIGSTY.el10.x86_64.rpm
| PIGSTY 0.3.0 el10.x86_64.pg16 : re2_16 re2_16-0.3.0-1PIGSTY.el10.x86_64.rpm
| PIGSTY MISS | PIGSTY MISS |
| el10.aarch64 | PIGSTY 0.3.0 el10.aarch64.pg18 : re2_18 re2_18-0.3.0-1PIGSTY.el10.aarch64.rpm
| PIGSTY 0.3.0 el10.aarch64.pg17 : re2_17 re2_17-0.3.0-1PIGSTY.el10.aarch64.rpm
| PIGSTY 0.3.0 el10.aarch64.pg16 : re2_16 re2_16-0.3.0-1PIGSTY.el10.aarch64.rpm
| PIGSTY MISS | PIGSTY MISS |
| d12.x86_64 | PIGSTY 0.3.0 d12.x86_64.pg18 : postgresql-18-re2 postgresql-18-re2_0.3.0-1PIGSTY~bookworm_amd64.deb
| PIGSTY 0.3.0 d12.x86_64.pg17 : postgresql-17-re2 postgresql-17-re2_0.3.0-1PIGSTY~bookworm_amd64.deb
| PIGSTY 0.3.0 d12.x86_64.pg16 : postgresql-16-re2 postgresql-16-re2_0.3.0-1PIGSTY~bookworm_amd64.deb
| PIGSTY MISS | PIGSTY MISS |
| d12.aarch64 | PIGSTY 0.3.0 d12.aarch64.pg18 : postgresql-18-re2 postgresql-18-re2_0.3.0-1PIGSTY~bookworm_arm64.deb
| PIGSTY 0.3.0 d12.aarch64.pg17 : postgresql-17-re2 postgresql-17-re2_0.3.0-1PIGSTY~bookworm_arm64.deb
| PIGSTY 0.3.0 d12.aarch64.pg16 : postgresql-16-re2 postgresql-16-re2_0.3.0-1PIGSTY~bookworm_arm64.deb
| PIGSTY MISS | PIGSTY MISS |
| d13.x86_64 | PIGSTY 0.3.0 d13.x86_64.pg18 : postgresql-18-re2 postgresql-18-re2_0.3.0-1PIGSTY~trixie_amd64.deb
| PIGSTY 0.3.0 d13.x86_64.pg17 : postgresql-17-re2 postgresql-17-re2_0.3.0-1PIGSTY~trixie_amd64.deb
| PIGSTY 0.3.0 d13.x86_64.pg16 : postgresql-16-re2 postgresql-16-re2_0.3.0-1PIGSTY~trixie_amd64.deb
| PIGSTY MISS | PIGSTY MISS |
| d13.aarch64 | PIGSTY 0.3.0 d13.aarch64.pg18 : postgresql-18-re2 postgresql-18-re2_0.3.0-1PIGSTY~trixie_arm64.deb
| PIGSTY 0.3.0 d13.aarch64.pg17 : postgresql-17-re2 postgresql-17-re2_0.3.0-1PIGSTY~trixie_arm64.deb
| PIGSTY 0.3.0 d13.aarch64.pg16 : postgresql-16-re2 postgresql-16-re2_0.3.0-1PIGSTY~trixie_arm64.deb
| PIGSTY MISS | PIGSTY MISS |
| u22.x86_64 | PIGSTY 0.3.0 u22.x86_64.pg18 : postgresql-18-re2 postgresql-18-re2_0.3.0-1PIGSTY~jammy_amd64.deb
| PIGSTY 0.3.0 u22.x86_64.pg17 : postgresql-17-re2 postgresql-17-re2_0.3.0-1PIGSTY~jammy_amd64.deb
| PIGSTY 0.3.0 u22.x86_64.pg16 : postgresql-16-re2 postgresql-16-re2_0.3.0-1PIGSTY~jammy_amd64.deb
| PIGSTY MISS | PIGSTY MISS |
| u22.aarch64 | PIGSTY 0.3.0 u22.aarch64.pg18 : postgresql-18-re2 postgresql-18-re2_0.3.0-1PIGSTY~jammy_arm64.deb
| PIGSTY 0.3.0 u22.aarch64.pg17 : postgresql-17-re2 postgresql-17-re2_0.3.0-1PIGSTY~jammy_arm64.deb
| PIGSTY 0.3.0 u22.aarch64.pg16 : postgresql-16-re2 postgresql-16-re2_0.3.0-1PIGSTY~jammy_arm64.deb
| PIGSTY MISS | PIGSTY MISS |
| u24.x86_64 | PIGSTY 0.3.0 u24.x86_64.pg18 : postgresql-18-re2 postgresql-18-re2_0.3.0-1PIGSTY~noble_amd64.deb
| PIGSTY 0.3.0 u24.x86_64.pg17 : postgresql-17-re2 postgresql-17-re2_0.3.0-1PIGSTY~noble_amd64.deb
| PIGSTY 0.3.0 u24.x86_64.pg16 : postgresql-16-re2 postgresql-16-re2_0.3.0-1PIGSTY~noble_amd64.deb
| PIGSTY MISS | PIGSTY MISS |
| u24.aarch64 | PIGSTY 0.3.0 u24.aarch64.pg18 : postgresql-18-re2 postgresql-18-re2_0.3.0-1PIGSTY~noble_arm64.deb
| PIGSTY 0.3.0 u24.aarch64.pg17 : postgresql-17-re2 postgresql-17-re2_0.3.0-1PIGSTY~noble_arm64.deb
| PIGSTY 0.3.0 u24.aarch64.pg16 : postgresql-16-re2 postgresql-16-re2_0.3.0-1PIGSTY~noble_arm64.deb
| PIGSTY MISS | PIGSTY MISS |
| u26.x86_64 | PIGSTY 0.3.0 u26.x86_64.pg18 : postgresql-18-re2 postgresql-18-re2_0.3.0-1PIGSTY~resolute_amd64.deb
| PIGSTY 0.3.0 u26.x86_64.pg17 : postgresql-17-re2 postgresql-17-re2_0.3.0-1PIGSTY~resolute_amd64.deb
| PIGSTY 0.3.0 u26.x86_64.pg16 : postgresql-16-re2 postgresql-16-re2_0.3.0-1PIGSTY~resolute_amd64.deb
| PIGSTY MISS | PIGSTY MISS |
| u26.aarch64 | PIGSTY 0.3.0 u26.aarch64.pg18 : postgresql-18-re2 postgresql-18-re2_0.3.0-1PIGSTY~resolute_arm64.deb
| PIGSTY 0.3.0 u26.aarch64.pg17 : postgresql-17-re2 postgresql-17-re2_0.3.0-1PIGSTY~resolute_arm64.deb
| PIGSTY 0.3.0 u26.aarch64.pg16 : postgresql-16-re2 postgresql-16-re2_0.3.0-1PIGSTY~resolute_arm64.deb
| PIGSTY MISS | PIGSTY MISS |
构建
您可以使用 pig build 命令构建 re2 扩展的 RPM / DEB 包:
pig build pkg re2 # 构建 RPM / DEB 包
安装
您可以直接安装 re2 扩展包的预置二进制包,首先确保 PGDG 和 PIGSTY 仓库已经添加并启用:
pig repo add pgsql -u # 添加仓库并更新缓存
使用 pig 或者是 apt/yum/dnf 安装扩展:
pig install re2; # 当前活跃 PG 版本安装
pig ext install -y re2 -v 18 # PG 18
pig ext install -y re2 -v 17 # PG 17
pig ext install -y re2 -v 16 # PG 16
dnf install -y re2_18 # PG 18
dnf install -y re2_17 # PG 17
dnf install -y re2_16 # PG 16
apt install -y postgresql-18-re2 # PG 18
apt install -y postgresql-17-re2 # PG 17
apt install -y postgresql-16-re2 # PG 16
创建扩展:
CREATE EXTENSION re2;
用法
来源:official README、official reference doc、v0.3.0 release
re2 提供由 Google RE2 引擎驱动、与 ClickHouse 兼容的正则表达式函数。它同时暴露 text 和 bytea 重载,因此也可以搜索包含 \\0 字节的二进制数据。Pigsty 为 PostgreSQL 16-18 打包版本 0.3.0,而上游文档说明支持 PostgreSQL 13+。
CREATE EXTENSION re2;
SELECT re2match('hello world', 'h.*o');
SELECT re2extract('Order #123', '(\\d+)');
SELECT re2countmatches('a1 b2 c3', '\\d');
核心函数
re2match(haystack, pattern) -> booleanre2extract(haystack, pattern) -> text|byteare2extractall(haystack, pattern) -> text[]|bytea[]re2regexpextract(haystack, pattern, index default 1) -> text|byteare2extractgroups(haystack, pattern) -> text[]|bytea[]re2extractallgroupsvertical(haystack, pattern) -> text[]|bytea[]re2extractallgroupshorizontal(haystack, pattern) -> text[]|bytea[]re2regexpquotemeta(haystack) -> text|byteare2splitbyregexp(pattern, haystack, max_substrings default 0) -> text[]|bytea[]re2replaceregexpone(haystack, pattern, replacement) -> text|byteare2replaceregexpall(haystack, pattern, replacement) -> text|byteare2countmatches(...)和re2countmatchescaseinsensitive(...)
SELECT re2extractallgroupsvertical('a=1 b=2', '(\\w)=(\\d)');
SELECT re2regexpquotemeta('a+b?');
SELECT re2splitbyregexp('\\s+', 'one two three', 2);
多模式匹配
re2multimatch* 系列既接受多个 pattern 参数,也接受 VARIADIC 数组:
SELECT re2multimatchany('error: timeout', 'timeout', 'denied');
SELECT re2multimatchanyindex('error: timeout', VARIADIC ARRAY['timeout', 'denied']);
SELECT re2multimatchallindices('error: timeout', 'error', 'timeout', 'panic');
匹配语义
- 为了匹配 ClickHouse 行为,
.默认会匹配换行。 - 如果希望
.不跨越换行,请在 pattern 前加上(?-s)。 - 替换字符串支持
\\0到\\9反向引用。
注意事项
- 上游要求构建/安装时系统中有
re2库。 - Release
v0.3.0使用 SQL version0.3;从更早 minor release 替换扩展二进制后,执行ALTER EXTENSION re2 UPDATE TO '0.3'。 re2splitbyregexp在v0.3.0中将参数顺序改为pattern, haystack[, max_substrings],与 ClickHouse 一致。早期0.2.0build 使用的是haystack, pattern。- 上游将 patch releases 视为 binary-only,但 minor releases 可能需要 SQL upgrade scripts。