MySQL
MySQL
SELECT REGEXP_SUBSTR('order-2024-0678-x', '[0-9]{4}-[0-9]{4}');
-- '2024-0678'PostgreSQL
PostgreSQL
-- regexp_match 返回捕获组数组(只匹配首处)
SELECT (regexp_match('order-2024-0678-x', '(\d{4})-(\d{4})'))[1]; -- '2024'
-- 只要整个匹配片段:
SELECT substring('order-2024-0678-x' from '\d{4}-\d{4}'); -- '2024-0678'| MySQL | REGEXP_SUBSTR(expr, pat[, pos[, occurrence[, match_type]]]) |
|---|---|
| PostgreSQL | regexp_match 返回 text[](无匹配为 NULL);PG 15+ 提供 regexp_substr |
| 无匹配 | MySQL 返回 NULL;regexp_match 返回 NULL |