我想更新仅包含电子邮件地址的域部分的列。
update
users
set
email_base_domain = regexp_match(email, '(\w+\.\w+)$')
但是,
regexp_match
返回text[]
。如果电子邮件是 [email protected]
,上面将 email_base_domain 设置为 {foo.com}
。我想要foo.com
。
如何从
text
返回的 text[]
中获取第一个元素 regexp_match
?
添加一组括号对要提取的内容进行分组。例如:
SELECT regexp_matches(email, '(\w+)\.\w+$')
FROM users
将返回
{foo}
,同时
SELECT regexp_matches(email, '\w+\.(\w+)$')
FROM users
将返回“{com}”。
一旦有了文本数组,您就可以使用常规数组索引从返回的文本数组中提取值。例如:
SELECT (regexp_matches(email, '(\w+)\.\w+$'))[1]
FROM users
返回
'foo'
和
SELECT (regexp_matches(email, '\w+\.(\w+)$'))[1]
FROM users
回归
'com'
。
如果 'regexp_match(email, '(\w+.\w+)$')' 返回 '{foo.com}' 但你只需要 'foo.com',你应该使用圆括号,如 ()[1]
update users
set email_base_domain = (regexp_match(email, '(\w+\.\w+)$'))[1]