繁体   English   中英

使用 Reg_exp 从电子邮件预言机中提取名字和姓氏

[英]Extracting First_name & Last_name from email oracle using Reg_exp

如何使用 oracle REGEXP_SUBSTR 从电子邮件中提取名字和姓氏,

电子邮件: susan.ryan@email.com

预期输出:

苏珊 瑞安
select
  substr('susan.ryan@email.com',1,(INSTR('susan.ryan@email.com','.')-1)) first_name,
  substr('susan.ryan@email.com',(INSTR('susan.ryan@email.com','.')+1),(INSTR('susan.ryan@email.com','@'))) last_name
from dual;

但我得到的结果是

苏珊 瑞安@电子邮件。

你有

substr(email, instr(email, '.') + 1, instr(email, '@')) as last_name

但是第二个参数不是结束位置,而是请求的长度,所以必须减去点的位置:

substr(email, instr(email, '.') + 1, instr(email, '@') - instr(email, '.') - 1) as last_name

顺便说一下,使用REGEXP_SUBSTR更容易:

regexp_substr(email, '[[:alpha:]]+', 1, 1) as first_name,
regexp_substr(email, '[[:alpha:]]+', 1, 2) as last_name

我们在这里寻找仅由电子邮件中的字母组成的子字符串。 对于 first_name 我们取第一个这样的字符串,对于 last_name 取第二个。 这当然依赖于您表中的所有电子邮件均由 firstname.lastname@domain 组成。

这是REGEXP_SUBSTR上的文档: https : REGEXP_SUBSTR

以下是有关如何执行此操作的一些示例。 首先使用SUBSTR (列“域”)或REGEXP (列“domain_regexp”)删除域,然后使用REGEXP_SUBSTR拆分域(列“no_domain”)之前的部分:

WITH samples AS
(
  SELECT '-susan.ryan@email.com' as str FROM DUAL UNION
  SELECT 'roger@email.com' as str FROM DUAL
)
SELECT 
str as email,
REGEXP_SUBSTR(str,'@.+$') AS domain_regexp,
SUBSTR(str, INSTR(str,'@')) as domain,
SUBSTR(str, 1, INSTR(str,'@') - 1) as no_domain,
REGEXP_SUBSTR(SUBSTR(str, 1, INSTR(str,'@') - 1),'[^.]+',1,1) AS first_name,
REGEXP_SUBSTR(SUBSTR(str, 1, INSTR(str,'@') - 1),'[^.]+',1,2) AS last_name
from samples;

EMAIL                 DOMAIN_REGEXP         DOMAIN                NO_DOMAIN             FIRST_NAME            LAST_NAME            
--------------------- --------------------- --------------------- --------------------- --------------------- ---------------------
-susan.ryan@email.com @email.com            @email.com            -susan.ryan           -susan                ryan                 
roger@email.com       @email.com            @email.com            roger                 roger                                      


暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM