idp*_*d15 4 regex string hive hiveql regexp-replace
我需要替换给定字符串中第一次出现的子字符串。
例如,如果字符串是"My name is Adam"并且我想将第一个替换"a"为"@".
所以我想要的输出是"My n@me is Adam".
在 MySQL 中,有一个函数regexp_replace有一个可选参数occurrence来指定要替换的出现次数。但不幸的是,该可选参数不存在于 hive 函数中。有什么建议么?
hive> select regexp_replace('My name is Adam','^(.*?)a','$1@');
OK
My n@me is Adam
Time taken: 0.061 seconds, Fetched: 1 row(s)
Run Code Online (Sandbox Code Playgroud)
图案的'^(.*?)a'意思是:
^ - the beginning of the string
.*? - any character (.) zero or more times (*) not greedy (?)
() - remember group, we will refer it in the replacement string as $1
a - 'a' character literally
Run Code Online (Sandbox Code Playgroud)
替换字符串的'$1@'意思是:
$1 - group number one in the pattern (everything before 'a')
@ - '@' character literally
Run Code Online (Sandbox Code Playgroud)
您可以在这里调试正则表达式:regex101.com