本文共 608 字,大约阅读时间需要 2 分钟。
VARCHAR REGEXP_EXTRACT(VARCHAR str, VARCHAR pattern, INT index)
str
VARCHAR类型,指定的字符串。
pattern
VARCHAR类型,匹配的字符串。
index
INT类型,第几个被匹配的字符串。
注意:正则常量请按照Java代码来写。codegen会将SQL常量字符串自动转化成Java代码。如果要描述一个数字(\d),需要写成 ‘\d’,也就是像在Java中写正则一样。
使用正则模式pattern匹配抽取字符串str中的第index个子串,index从1开始,正则匹配提取。参数为null或者正则不合法返回null。
str1 (VARCHAR) | pattern1(VARCHAR) | index1 (INT) |
---|---|---|
foothebar | foo(.*?)(bar) | 2 |
100-200 | (\\d+)-(\\d+) | 1 |
null | foo(.*?)(bar) | 2 |
foothebar | null | 2 |
foothebar | 空 | 2 |
foothebar | ( | 2 |
SELECT REGEXP_EXTRACT(str1, pattern1, index1) as result
FROM T1
result(VARCHAR) |
---|
bar |
100 |
null |
null |
null |
null |
转载地址:http://ytvml.baihongyu.com/