【问题标题】:Hive Regex_replace is giving Could not compile regexp pattern: errorHive Regex_replace 给出了无法编译正则表达式模式:错误
【发布时间】:2023-03-14 11:37:01
【问题描述】:

我正在尝试替换我的字符串中的“[[”和“]]”,并且我正在使用两个 regex_replace 函数来做到这一点。

Select regexp_replace(regexp_replace("[[2020-03-23T10:33:03Z, 224554, training, 2019-05-06T15:53:23Z, Completed]]"
,"]]",""),"[[","")

当我尝试替换第一个字符串“]]”时它工作正常,但如果我对“[[”执行相同操作,则会出现编译错误:

无法编译正则表达式模式:[[错误:缺少]:[[

任何帮助将不胜感激

【问题讨论】:

  • 可以正常使用replace吗? reg replace 可能正在考虑将 [[ 作为其关键字的一部分。
  • replace 不适用于我们拥有的 hive 版本,translate 的工作方式与 replace 相同。

标签: regex hive hiveql impala regexp-replace


【解决方案1】:

[] 字符在正则表达式中具有特殊含义,应使用双反斜杠进行转义。您也可以使用 OR (|) 将 [[]] 组合在单个 regexp_replace 中:

Select regexp_replace("[[2020-03-23T10:33:03Z, 224554, training, 2019-05-06T15:53:23Z, Completed]]"
,"\\]\\]|\\[\\[","")

结果:

2020-03-23T10:33:03Z, 224554, training, 2019-05-06T15:53:23Z, Completed

除了两个方括号,您可以使用单个带量词 \\]{2}\\]+ 的方括号来表示任意数量的括号

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-28
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多