【问题标题】:Hive: remove the special characters and keep the space between the wordHive:删除特殊字符并保留单词之间的空格
【发布时间】:2018-12-01 03:53:03
【问题描述】:

我有自定义表格,并且此列中有特殊字符。我想删除特殊字符并保留单词之间的空格。

我试试这个查询。

从客户中选择 customer_ID, REGEXP_REPLACE(name, '[^0-9A-Za-z]', '')

但是这个查询会删除所有特殊字符和空格。

如何特殊字符并在此列中保留单词之间的空格?

【问题讨论】:

    标签: regex string hive space


    【解决方案1】:

    如果您想保留空格,则可以将其添加到否定字符类中。

    要匹配字符类一次或多次,您可以在字符类后添加 + 号。

    [^0-9A-Za-z ]+

    您的查询将如下所示:

    select customer_ID, REGEXP_REPLACE(name, '[^0-9A-Za-z ]+', '') from customer

    【讨论】:

    • select customer_ID, REGEXP_REPLACE(name, '[^0-9A-Za-z ]+', '') from customer 我收到错误消息 Invalid function 'EGEXP_REPLACE'
    • 无效函数'EGEXP_REPLACE'。那是错字吗?你可以试试你的原始代码,只在字符类中添加一个空格吗?
    • 您好,第四只鸟,您能否更新整个查询,我会接受它作为答案。从客户中选择 customer_ID, REGEXP_REPLACE(name, '[^0-9A-Za-z ]+', '')
    • @Anson 假设 select customer_ID, REGEXP_REPLACE(name, '[^0-9A-Za-z ]+', '') from customer 为您工作,我已将其添加到我的答案中。