Impala 正则表达式先行和后行的解决方法

Workaround for Impala Regex lookahead and lookbehind

如果我使用 Hive,下面的工作正常。但是如果我使用 Impala,它会抛出错误:

select regexp_replace("foobarbarfoo","bar(?=bar)","<NA>");

WARNINGS: Could not compile regexp pattern: bar(?=bar)
Error: invalid perl operator: (?=

基本上Impala不支持前瞻和后视

https://www.cloudera.com/documentation/enterprise/release-notes/topics/impala_incompatible_changes.html#incompatible_changes_200

今天有解决方法吗?也许使用 UDF?

谢谢。

由于您使用的是 regexp_replace,因此匹配并捕获要保留的字符串部分(但要用作必备上下文)并替换为反向引用。见 regexp_replace Impala reference:

These examples show how you can replace parts of a string matching a pattern with replacement text, which can include backreferences to any () groups in the pattern string. The backreference numbers start at 1, and any \characters must be escaped as \.

所以,在这里,您可以使用

select regexp_replace("foobarbarfoo","bar(bar)","<NA>\1");
                                         ^   ^       ^^^   

请注意,它不会替换连续的匹配项,但是,它会在当前场景和 foobarbarfoo will turn into foo<NA>barfoo 中工作(请注意,Go 正则表达式引擎也是 RE2,因此在 [=27= 时选择此选项]).