語法
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 |