hive--去除特殊字符regexp_replace

regexp_replace

'\t abc \n def \r hij'' abc def hij'

select regexp_replace('\t abc \n def \r hij', '\n|\t|\r|', ''); 
  • ' abc def hij'

'\t abc \n def \r hij''abcdefhij'

select regexp_replace('\t abc \n def \r hij', '\n|\t|\r| |', '');
  • 'abcdefhij'
  • 解释
    • \t:tab,跳格(移至下一列)
    • \r:回车
    • \n:换行
select regexp_replace('a[]b','\\[|\\]','x');
-- axxb


select regexp_replace('a[[]]b','\\[\\[|\\]\\]','x');
-- axxb

看一下其他的情况

  • select trim('中文空格 dsfs ');
    • '中文空格 dsfs'
  • select trim('英文空格 dsfs ');
    • '英文空格 dsfs'
  • select replace('asdas dsfs ',' ','');
    • SemanticException Line 0:-1 Invalid function 'replace'
  • select regexp_replace('中文空格 dsfs ',' ','');
    • '中文空格dsfs'
  • select regexp_replace('英文空格 dsfs ',' ','');
    • '英文空格dsfs'
  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Hive有三种字符串替换函数:replace、translate和regexp_replace。 - replace函数用于替换指定字符串的某个子字符串,可以直接指定要替换的字符串,比较简单易用。 - translate函数用于替换指定字符串的一组字符为另一组字符,适用于替换单个字符的场景,比如大小写转换、字符去重等。 - regexp_replace函数用于使用正则表达式匹配指定字符串的子字符串,并将其替换为另一个字符串,适用于比较复杂的替换场景,例如替换多个子字符串、替换特定模式的字符串等。 下面是三个函数的语法和示例: - replace函数: ``` replace(string input, string search, string replacement) ``` 例如,将"hello world"的"o"替换为"e",可以使用如下语句: ``` SELECT replace("hello world", "o", "e"); ``` 执行结果为:"helle werld"。 - translate函数: ``` translate(string input, string from, string to) ``` 例如,将"hello world"的字母o替换为e,可以使用如下语句: ``` SELECT translate("hello world", "o", "e"); ``` 执行结果为:"helle werld"。 - regexp_replace函数: ``` regexp_replace(string input, string pattern, string replacement) ``` 例如,将"hello world"的所有字母o替换为e,可以使用如下语句: ``` SELECT regexp_replace("hello world", "o", "e"); ``` 执行结果为:"helle werld"。需要注意的是,pattern参数可以使用正则表达式,因此可以实现更加灵活的字符串替换操作。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值