更新时间:2023-09-27 GMT+08:00
regexp_substr
regexp_substr函数用于计算从start_position位置开始,source中第occurrence次匹配指定pattern的子串。
命令格式
regexp_substr(string <source>, string <pattern>[, bigint <start_position>[, bigint <occurrence>]])
参数说明
参数 | 是否必选 | 参数类型 | 说明 |
|---|---|---|---|
source | 是 | STRING | 待搜索的字符串。 |
pattern | 是 | STRING | STRING类型常量或正则表达式。待匹配的模型。 |
start_position | 否 | BIGINT | 起始位置,必须大于0。不指定时默认为1,表示从source的第一个字符开始匹配。 |
occurrence | 否 | BIGINT | BIGINT常量,必须大于0。不指定时默认为1,表示返回第一次匹配的子串。 |
返回值说明
返回STRING类型的值。

- 如果pattern为空串,返回报错。
- 没有匹配时,返回NULL。
- start_position或occurrence非BIGINT类型或小于等于0时,返回报错。
- source、pattern、start_position、occurrence或return_option值为NULL时,返回NULL。
示例代码
返回a。
select regexp_substr('a1b2c3', '[a-z]'); 返回b。
select regexp_substr('a1b2c3', '[a-z]', 2, 1); 返回c。
select regexp_substr('a1b2c3', '[a-z]', 2, 2); 返回NULL。
select regexp_substr('a1b2c3', null); 父主题:字符串函数

