select REGEXP_REPLACE('佰分贰拾(深圳)品牌管理有限公司', '\\(', '(')
![](https://img-blog.csdnimg.cn/f2ad2975141241debf55d4a605929e3d.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAY2Nsb3ZlemJm,size_20,color_FFFFFF,t_70,g_se,x_16)
然后
SELECT vendor_full_name,REGEXP_REPLACE(vendor_full_name, '\\(', '(')
FROM odsmdmdata.sms_vendor_party_t
![](https://img-blog.csdnimg.cn/5e3213279ac4416cb94401a4db26e60c.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAY2Nsb3ZlemJm,size_20,color_FFFFFF,t_70,g_se,x_16)
报错,这就日了狗了 仔细看报错日志 我就两个\\ 怎么就被转义成4个\\了
所以我只用一个\就好
SELECT vendor_full_name,REGEXP_REPLACE(vendor_full_name, '\(', '(')
FROM odsmdmdata.sms_vendor_party_t
where sms_vendor_id ='40539';
![](https://img-blog.csdnimg.cn/ff1f82515c8a4779ad27609cb0588fff.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAY2Nsb3ZlemJm,size_20,color_FFFFFF,t_70,g_se,x_16)
接着研究下 REGEXP_REPLACE 函数
Impala String Functions | 5.9.x | Cloudera Documentation
https://docs.cloudera.com/documentation/enterprise/5-9-x/topics/impala_string_functions.html
[localhost:21000] > select regexp_replace('aaabbbaaa','b+','xyz');
+------------------------------------------+
| regexp_replace('aaabbbaaa', 'b+', 'xyz') |
+------------------------------------------+
| aaaxyzaaa |
+------------------------------------------+
Returned 1 row(s) in 0.11s
这里好理解 b+只得是1个b或者多个b 这里匹配到了bbb 所以bbb替换到xyz ,so easy
[localhost:21000] > select regexp_replace('aaabbbaaa','(b+)','<\\1>');
+----------------------------------------------+
| regexp_replace('aaabbbaaa', '(b+)', '<\\1>') |
+----------------------------------------------+
| aaa<bbb>aaa |
+----------------------------------------------+
Returned 1 row(s) in 0.11s
这里(b+) 指的是 bbb这三个整体 注意这个括号不能省略, <\\1>中的\\就是上文bbb ,<>就是包围
select
regexp_replace('aaabbbaaa','(b+)','<\\1>'),
regexp_replace('aaabbbaaa','b+','<\\1>'), --验证必须有()
regexp_replace('aaabbbaaa','(b+)','【\\1】'),--验证<>可以随意替换
regexp_replace('aaabbbaaa','a(b+)','【\\1】')--验证只有括号里的才有用
[localhost:21000] > select regexp_replace('123-456-789','[^[:digit:]]','');
+---------------------------------------------------+
| regexp_replace('123-456-789', '[^[:digit:]]', '') |
+---------------------------------------------------+
| 123456789 |
+---------------------------------------------------+
[[:digit:]]代表数字,这种写法是为了避免转义字符, ^是非的意思
验证
select regexp_replace('123-456-789','[^\\d+]',''),
regexp_replace('123-456-789','[[:digit:]]','')
以上3个demo都很简单,但是我网上查到一个突然就有点懵了
SELECT REGEXP_REPLACE('(jackie)&|(jackie chan)', '\\(|\\)|(|)', ' ')
乍一看好像很复杂。。
实际上还好。
\\(|\\)|(|)' 其实可以看作 \\( | \\) | (|) 也就是\\( = ( ,\\)=) ,|=或的意思 ,(|)这个是啥呢?经过我测试(|)=()=两个字符中间没有空的那个没空,验证如下
SELECT REGEXP_REPLACE('(jackie)&|(jackie chan)', '\\(|\\)|(|)', ' ') ,
REGEXP_REPLACE('(jackie)&|(jackie chan)', '\\(|\\)|()', ' ') ,--这个为了说明(|)=()
REGEXP_REPLACE('(jackie)&|(jackie chan)', '\\(|\\)', ' ') ,--这个为了说明这个正则就是匹配()把它替换空格
REGEXP_REPLACE('(jackie)&|(jackie chan)', '()', '-') -- 这个为了说明()=无空
——————————————————————————————————————————
顺道学习点函数
split_part
select split_part('one***two***three','***',2);
+-------------------------------------------+
| split_part('one***two***three', '***', 2) |
+-------------------------------------------+
| two |
+-------------------------------------------+
select split_part('one\|/two\|/three','\|/',3);
+-------------------------------------------+
| split_part('one\|/two\|/three', '\|/', 3) |
+-------------------------------------------+
| three |
+-------------------------------------------+
translate
select translate ('hello world','world','earth');
+--------------------------------------------+
| translate('hello world', 'world', 'earth') |
+--------------------------------------------+
| hetta earth |
+--------------------------------------------+
我有另外一篇文章专门解释translate 暂不说了。
说下其他的 中文乱码, 但是replace 是可以的。暂无解决办法
![](https://img-blog.csdnimg.cn/f0f8441543a348528875bf2ea14eedac.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAY2Nsb3ZlemJm,size_20,color_FFFFFF,t_70,g_se,x_16)
![](https://img-blog.csdnimg.cn/8c2b2517ffc646089f8fbecb73fa719f.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAY2Nsb3ZlemJm,size_20,color_FFFFFF,t_70,g_se,x_16)
--未完待续