regexp_matches() 是一个系统函数,用于返回与 POSIX 正则表达式匹配的子字符串。
regexp_matches() 在 PostgreSQL 8.3 中添加。
用法
regexp_matches (stringtext,patterntext[,flagstext] ) → setoftext[]
命名参数从 PostgreSQL 18 开始可用。
通常,提供至少一个标志是有意义的,特别是 'g' 标志,否则 regexp_matches() 的行为将与 regexp_match() 完全相同。
除了 'g' 之外的其他标志列表可在 PostgreSQL 文档中找到:ARE 嵌入式选项字母。
变更历史
- PostgreSQL 18
- 添加了对参数名称的支持(提交 580f8727)
- PostgreSQL 8.3
- 添加(提交 9eb78bee)
示例
regexp_matches() 的基本用法示例
postgres=# SELECT regexp_matches('foobarboo', '.oo', 'g');
regexp_matches
----------------
{foo}
{boo}
(2 rows)
检索多个匹配项
postgres=# SELECT regexp_matches('foobarboo floobiloo', '(.oo).+?(.oo)', 'g');
regexp_matches
----------------
{foo,boo}
{loo,loo}
(2 rows)
请注意,不带 'g' 标志使用 regexp_matches() 等同于 regexp_match()。
postgres=# SELECT regexp_match('foobarboo', '.oo'), regexp_matches('foobarboo', '.oo');
regexp_match | regexp_matches
--------------+----------------
{foo} | {foo}
(1 row)
参考资料
- PostgreSQL 文档: 其他字符串函数
- PostgreSQL 文档: POSIX 正则表达式
