REGEXP_REPLACE 捕获组
REGEXP_REPLACE capturing groups
我想知道是否有人可以帮助我了解如何使用 Hive 的 regexp_replace 函数来捕获正则表达式中的组并在替换字符串中使用这些组。
我有一个我正在解决的示例问题,涉及日期修改。在此示例中,我的目标是采用与 SimpleDateFormat 解析不兼容的字符串日期并进行小幅调整以使其兼容。日期字符串(如下所示)需要在字符串中的偏移符号 (+/-) 前加上 "GMT"。
因此,给定输入:
'2015-01-01 02:03:04 +0:00'
-or-
'2015-01-01 02:03:04 -1:00'
我要输出:
'2015-01-01 02:03:04 GMT+0:00'
-or-
'2015-01-01 02:03:04 GMT-1:00'
这是一个简单的语句示例,我 'thought' 可以工作,但我得到了奇怪的输出。
Hive 查询:
select regexp_replace('2015-01-01 02:03:04 +0:00', ' ([+-])', ' GMT');
实际结果:
2015-01-01 02:03:04 GMT10:00
注意“\1”应该输出匹配的组,而是用数字“1”替换匹配的组。
谁能帮我理解 reference/output 替换字符串中匹配组的正确方法?
谢谢!
Hive 支持的表示法(至少对于 0.14,我想我记得 0.13.x 也是这样)对于正则表达式反向引用似乎是 </code> 对于捕获组 1, <code>
用于捕获组 2,等等。看起来它是基于(甚至可能由)replaceAll method from the Matcher class 实现的。这是该文档的相关部分:
Dollar signs may be treated as references to captured subsequences as described above, and backslashes are used to escape literal characters in the replacement string.
所以我想你想要的是这个:
select regexp_replace('2015-01-01 02:03:04 +0:00', ' ([+-])', ' GMT');
例如:
hive> select regexp_replace('2015-01-01 02:03:04 +0:00', ' ([+-])', ' GMT');
OK
2015-01-01 02:03:04 GMT+0:00
Time taken: 0.072 seconds, Fetched: 1 row(s)
hive> select regexp_replace('2015-01-01 02:03:04 -1:00', ' ([+-])', ' GMT');
OK
2015-01-01 02:03:04 GMT-1:00
Time taken: 0.144 seconds, Fetched: 1 row(s)
在尝试 back-reference REGEXP_REPLACE 中捕获的组时,'\1' 和 '$1' 对我都不起作用。
然而这有效:
https://www.logicbig.com/tutorials/core-java-tutorial/java-regular-expressions/group-ref-in-replacement.html
示例:(用连字符替换散列)
hive> select REGEXP_REPLACE('foo#bar','(?<tag1>foo)#(?<tag2>bar)', '${tag1}-${tag2}');
OK
foo-bar
Time taken: 0.085 seconds, Fetched: 1 row(s)
Hope this is helpful.
我想知道是否有人可以帮助我了解如何使用 Hive 的 regexp_replace 函数来捕获正则表达式中的组并在替换字符串中使用这些组。
我有一个我正在解决的示例问题,涉及日期修改。在此示例中,我的目标是采用与 SimpleDateFormat 解析不兼容的字符串日期并进行小幅调整以使其兼容。日期字符串(如下所示)需要在字符串中的偏移符号 (+/-) 前加上 "GMT"。
因此,给定输入:
'2015-01-01 02:03:04 +0:00'
-or-
'2015-01-01 02:03:04 -1:00'
我要输出:
'2015-01-01 02:03:04 GMT+0:00'
-or-
'2015-01-01 02:03:04 GMT-1:00'
这是一个简单的语句示例,我 'thought' 可以工作,但我得到了奇怪的输出。
Hive 查询:
select regexp_replace('2015-01-01 02:03:04 +0:00', ' ([+-])', ' GMT');
实际结果:
2015-01-01 02:03:04 GMT10:00
注意“\1”应该输出匹配的组,而是用数字“1”替换匹配的组。
谁能帮我理解 reference/output 替换字符串中匹配组的正确方法?
谢谢!
Hive 支持的表示法(至少对于 0.14,我想我记得 0.13.x 也是这样)对于正则表达式反向引用似乎是 </code> 对于捕获组 1, <code>
用于捕获组 2,等等。看起来它是基于(甚至可能由)replaceAll method from the Matcher class 实现的。这是该文档的相关部分:
Dollar signs may be treated as references to captured subsequences as described above, and backslashes are used to escape literal characters in the replacement string.
所以我想你想要的是这个:
select regexp_replace('2015-01-01 02:03:04 +0:00', ' ([+-])', ' GMT');
例如:
hive> select regexp_replace('2015-01-01 02:03:04 +0:00', ' ([+-])', ' GMT');
OK
2015-01-01 02:03:04 GMT+0:00
Time taken: 0.072 seconds, Fetched: 1 row(s)
hive> select regexp_replace('2015-01-01 02:03:04 -1:00', ' ([+-])', ' GMT');
OK
2015-01-01 02:03:04 GMT-1:00
Time taken: 0.144 seconds, Fetched: 1 row(s)
在尝试 back-reference REGEXP_REPLACE 中捕获的组时,'\1' 和 '$1' 对我都不起作用。 然而这有效: https://www.logicbig.com/tutorials/core-java-tutorial/java-regular-expressions/group-ref-in-replacement.html
示例:(用连字符替换散列)
hive> select REGEXP_REPLACE('foo#bar','(?<tag1>foo)#(?<tag2>bar)', '${tag1}-${tag2}');
OK
foo-bar
Time taken: 0.085 seconds, Fetched: 1 row(s)
Hope this is helpful.