在Hive中判断字符串中是否包含指定子字符串

方法一 : like

1.1 传统的 like 查找:

select 'this 是 china' like('%是%');	--true
select 'this 是 china' like '%是%';		--true

select 'this 是 china' like('%否%');	--false
select 'this 是 china' like '%否%';		--false

1.2 基于正则表达式的查找:

参考java正则表达式的用法:

select 'this 是 china' rlike('是');		--true

select 'foobar' rlike('foo');			--true	
select 'foobar' rlike('bar');			--true
select 'foobar' rlike('^f.*r$');		--true

方法二 : locate

用法:

返回值 函数名 函数说明
int locate(string substr, string str[, int pos]) [一、参数说明: 参数1-substr: 待查找的字符子串; 参数2-str: 原始字符串; 参数3-pos: 指定位置,将查找在此位置及之后位置第一次出现指定字符串的位置.] …[英文说明: Returns the position of the first occurrence of substr in str after position pos.]

使用案例:

select locate('i','this is china'); 	--3
select locate('i','this is china',4);	--6
select locate('i','this is china',7);	--11

方法三 : regexp

用法:

运算符 左右操作数类型 函数说明
A REGEXP B strings 属于关系运算符,同rlike函数, 根据java正则表达式判断. [一、参数说明: 参数1-A: 原始字符串; 参数2-B: 待比较的子字符串] …[英文说明:NULL if A or B is NULL, TRUE if any (possibly empty) substring of A matches the Java regular expression B, otherwise FALSE. For example, ‘foobar’ RLIKE ‘foo’ evaluates to TRUE and so does ‘foobar’ RLIKE ‘^f.*r$’.]
select 'this 是 china' regexp('是'); --true

--以下2种写法等价
select 'this 是 china' regexp('否'); --false
select 'this 是 china' regexp '否';  --false

猜你喜欢

转载自blog.csdn.net/liuwei0376/article/details/123083587