我需要从给定的字符串中替换第一个出现的子字符串。
例如,如果字符串是"My name is Adam"
,而我想用"@"
替换第一个"a"
。
所以我想要的输出是"My n@me is Adam"
。
在MySQL中,有一个函数regexp_replace
,它有一个可选的参数occurrence
,用于指定要替换的匹配项的数量。但不幸的是,该可选参数不存在于hive函数中。有什么建议吗?
发布于 2019-08-05 05:50:20
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)
Pattern '^(.*?)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
替换字符串'$1@'
表示:
$1 - group number one in the pattern (everything before 'a')
@ - '@' character literally
您可以在以下位置调试regexp:regex101.com
https://stackoverflow.com/questions/57352664
复制相似问题