Regex 配置单元查询,仅替换子字符串的第一个匹配项
我需要替换给定字符串中第一个出现的子字符串。Regex 配置单元查询,仅替换子字符串的第一个匹配项,regex,string,hive,hiveql,regexp-replace,Regex,String,Hive,Hiveql,Regexp Replace,我需要替换给定字符串中第一个出现的子字符串。 例如,如果字符串是“我的名字是亚当”,我想用“@”替换第一个“a” 所以我想要的输出是我的n@me是亚当吗? 在MySQL中,有一个函数regexp\u replace,它有一个可选参数occurrence,用于指定要替换的实例数。但不幸的是,该可选参数在配置单元函数中不存在。有什么建议吗 hive> select regexp_replace('My name is Adam','^(.*?)a','$1@'); OK My n@me is
例如,如果字符串是
“我的名字是亚当”
,我想用“@”
替换第一个“a”
所以我想要的输出是我的n@me是亚当吗?在MySQL中,有一个函数
regexp\u replace
,它有一个可选参数occurrence
,用于指定要替换的实例数。但不幸的是,该可选参数在配置单元函数中不存在。有什么建议吗
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)
模式“^(.*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 - group number one in the pattern (everything before 'a')
@ - '@' character literally
替换字符串“$1@”
表示:
^ - 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 - group number one in the pattern (everything before 'a')
@ - '@' character literally
您可以在此处调试regexp: