Oracle : REGEXP_REPLACE 在一个子字符串中

Oracle : REGEXP_REPLACE in a substring

我需要替换电子邮件地址中的一些字符。例如:

如果我的电子邮件地址是:patty.beads@mydomain.com,输出应该是:

pattybeads@mydomain.com <-- removed '.' before '@mydomain.com'

同样,如果我的电子邮件地址是 patty.beads+something@mydomain.com,输出应该是:

patty.beads+something@mydomain.com <-- removed '.' and '+' before '@mydomain.com'

因此,应该删除@mydomain.com 之前的某些字符(如'.'、'+' 等)。虽然我看到我可以使用 REGEXP_REPLACE 来替换那些字符,但很难理解如何包含替换必须在 '@' 之前的子字符串中的约束。

我可以得到任何帮助吗?

我想你要找的是这个 How to match "anything up until this sequence of characters" in a regular expression?

哪个 Oracle 没有在数据库中实现(据我所知)

所以这意味着根据 @ 将其拼接,从第一部分中删除 .|+ ,然后重新连接在一起。

SQL> with q as ( select 'patty.beads+something@mydomain.com' email from dual ) 
    select 
          REGEXP_REPLACE(substr(q.email,0,instr(q.email,'@')-1),'\.|\+','',1,0) 
          ||
          substr(q.email,instr(q.email,'@')) new_email
        from q;

NEW_EMAIL                       
--------------------------------
pattybeadssomething@mydomain.com

您可以拆分域名和名称,然后在名称上应用 REGEXP_REPLACETRANSLATE

另一种选择是使用 INSTRSUBSTR

SQL Fiddle

Oracle 11g R2 架构设置:

create table yourtable as
select 'pattybeads@mydomain.com' emailid FROM DUAL UNION ALL
select 'patty.beads@mydomain.com' FROM DUAL UNION ALL
select 'patty.beads+something@mydomain.com' FROM DUAL

查询 1:

select TRANSLATE (name, 'a.+', 'a' ) ||domain emailid
FROM
( SELECT 
    REGEXP_SUBSTR ( emailid, '(.+@)(.+)' ,1,1,NULL,1) name,
    REGEXP_SUBSTR ( emailid, '(.+)@(.+)' ,1,1,NULL,2) domain 
FROM yourtable
 )

Results:

|                          EMAILID |
|----------------------------------|
|          pattybeads@mydomain.com |
|          pattybeads@mydomain.com |
| pattybeadssomething@mydomain.com |

查询 2:

 select TRANSLATE ( SUBSTR (emailid,1,INSTR( emailid,'@' ) -1 ) ,
          'a.+', 'a' )||
         SUBSTR (emailid,INSTR( emailid,'@'  ) ) emailid
 FROM yourtable

Results:

|                          EMAILID |
|----------------------------------|
|          pattybeads@mydomain.com |
|          pattybeads@mydomain.com |
| pattybeadssomething@mydomain.com |

如果我没理解错的话,您想在电子邮件地址的第一部分只留下字母(可能是数字?)。如果是这样,这个怎么样?

REGEXP_REPLACE 中,\W 代表一个 非单词 ,所以 - 任何不是字母、数字或下划线的东西 (因为它被认为是 word) 正在被删除。为了也删除下划线,添加了 |_ - 最后 - 它被读作 删除非单词或下划线 .

SQL> with test (email) as (
  2    select 'pattybeads@mydomain.com'            from dual union all
  3    select 'patty.beads@mydomain.com'           from dual union all
  4    select 'patty.beads+something@mydomain.com' from dual union all
  5    select 'patty-beads$something@mydomain.com' from dual union all
  6    select 'pat_ty#b.e?a!d*s@mydomain.com'       from dual
  7  )
  8  select email,
  9    regexp_replace(substr(email, 1, instr(email, '@')), '\W|_', '') ||
 10    substr(email, instr(email, '@')) result
 11  from test;

EMAIL                              RESULT
---------------------------------- ----------------------------------------
pattybeads@mydomain.com            pattybeads@mydomain.com
patty.beads@mydomain.com           pattybeads@mydomain.com
patty.beads+something@mydomain.com pattybeadssomething@mydomain.com
patty-beads$something@mydomain.com pattybeadssomething@mydomain.com
pat_ty#b.e?a!d*s@mydomain.com      pattybeads@mydomain.com

SQL>