HIVE常用正則函數(like、rlike、regexp、regexp_replace、regexp_extract)


Oralce中regex_like和hive的regexp對應

LIKE

語法1: A LIKE B
語法2: LIKE(A, B)
操作類型: strings
返回類型: boolean或null
描述: 如果字符串A或者字符串B為NULL,則返回NULL;如果字符串A符合表達式B的正則語法,則為TRUE;否則為FALSE。B中字符"_"表示任意單個字符,而字符"%"表示任意數量的字符。

hive> select 'football' like '%ba';
OK
false
 
 
hive> select 'football' like '%ba%';
OK
true
 
 
hive> select 'football' like '__otba%';
OK
true
 
 
hive> select like('football', '__otba%');
OK
true

RLIKE

語法1: A RLIKE B
語法2: RLIKE(A, B)
操作類型: strings
返回類型: boolean或null
描述: 如果字符串A或者字符串B為NULL,則返回NULL;如果字符串A符合JAVA正則表達式B的正則語法,則為TRUE;否則為FALSE。

hive> select 'football' rlike 'ba';
OK
true
 
 
hive> select 'football' rlike '^footba';
OK
true
 
 
hive> select rlike('football', 'ba');
OK
true

Java正則:

"." 任意單個字符
"*" 匹配前面的字符0次或多次
"+" 匹配前面的字符1次或多次
"?" 匹配前面的字符0次或1次
"\d" 等於 [0-9],使用的時候寫成'\d'
"\D" 等於 [^0-9],使用的時候寫成'\D'

hive> select 'does' rlike 'do(es)?';
OK
true

hive> select '\\';
OK
\

hive> select '2314' rlike '\\d+';
OK
true

REGEXP

語法1: A REGEXP B
語法2: REGEXP(A, B)
操作類型: strings
返回類型: boolean或null
描述: 功能與RLIKE相同

hive> select 'football' regexp 'ba';
OK
true


hive> select 'football' regexp '^footba';
OK
true


hive> select regexp('football', 'ba');
OK
true


語法: regexp_replace(string A, string B, string C)
操作類型: strings
返回值: string
說明: 將字符串A中的符合java正則表達式B的部分替換為C。

hive> select regexp_replace('h234ney', '\\d+', 'o');
OK
honey

REGEXP_REPLACE

語法: regexp_replace(string A, string B, string C)
操作類型: strings
返回值: string
說明: 將字符串A中的符合java正則表達式B的部分替換為C。

hive> select regexp_replace('h234ney', '\\d+', 'o');
OK
honey

REGEXP_EXTRACT

語法: regexp_extract(string A, string pattern, int index)
返回值: string
說明:將字符串A按照pattern正則表達式的規則拆分,返回index指定的字符,index從1開始計。

hive> select regexp_extract('honeymoon', 'hon(.*?)(moon)', 0);
OK
honeymoon
 
 
hive> select regexp_extract('honeymoon', 'hon(.*?)(moon)', 1);
OK
ey
 
 
hive> select regexp_extract('honeymoon', 'hon(.*?)(moon)', 2);
OK
moon
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM