如何替换所有电子邮件的域名

时间:2018-09-27 21:18:54

标签: regex oracle plsql oracle10g plsqldeveloper

我正尝试用特定的电子邮件域替换任何电子邮件的结尾。该过程将传递两个字符串:第一是要替换的原始域名;第二是原始域名。第二个是将替换原始域名的新域名(按此顺序)。例如,如果将字符串“ sadme.com”和“ happyme.com”传递给该程序,则数据库中每个以“ sadme.com”为域名的电子邮件都将更改为“ happyme.com”。域名

示例:

Before                     After
-------------------------- ---------------------------
ExampleEmail@yahoo.com     ExampleEmail@gmail.com
SecondExample@hotmail.com  SecondExample@gmail.com

这是我所拥有的,但是我遇到了错误

          Create Procedure PR_Q3
    (P_OldDomain Varchar2,P_NewDomain Varchar2)
    As
      Cursor C_Domains IS Select Email_Address


       From Broker 
                     where Email_Address =  '%@'||P_OldDomain;
               V_OldDomain Varchar2(50);       
          Begin
          Open C_Domains;
          Fetch C_Domains INTO V_OldDomain;
          While C_Domains%Found loop
                if C_Domains = '%@'||P_OldDomain Then
                    Update Broker
                    Set Email_Address = P_NewDomain
                    Where Email_Address = V_OldDomain;
              End if;      
          Fetch C_Domains into P_NewDomain;
          End Loop;
  Close C_Domains;
End PR_Q3;
/
Show Errors;

我得到了错误:

LINE / COL ERROR


2/29 PLS-00103:在预期以下情况之一时遇到符号“(”:

     := . ) , @ % default character
     The symbol ":=" was substituted for "(" to continue.

2/61 PLS-00103:预期以下情况之一时遇到符号“(”:

     := . ) , @ % default character
     The symbol ":=" was substituted for "(" to continue.

我也尝试取出varchar2(50)并将Varchar2放进去,这给了我错误:

 PLS-00403: expression 'P_NewDomain' cannot be used as an INTO-target of a SELECT/FETCH statement

谢谢!

3 个答案:

答案 0 :(得分:2)

从位置1开始,用'@'替换不是bananas.com的第二组字符:

regexp_replace(email, '[^@]+', 'bananas.com', 1, 2)

或仅将'@'之后的所有内容替换为@bananas.com

regexp_replace(email, '@.+*','@bananas.com')

答案 1 :(得分:1)

我在Oracle 10 DB上运行了以下命令。希望这是您需要的:

WITH
    sampledata AS
        (SELECT 'finger@doctors.com' AS eaddress FROM DUAL
         UNION ALL
         SELECT 'toe.on.foon@real.goofy.org' FROM DUAL
         UNION ALL
         SELECT 'facenose@skeleton.usa' FROM DUAL)
SELECT eaddress
     , REGEXP_REPLACE (eaddress, '^(.+@).+(\..+$)', '\1gmail\2') t
  FROM sampledata;

eaddress                     t
finger@doctors.com          finger@gmail.com
toe.on.foon@real.goofy.org  toe.on.foon@gmail.org
facenose@skeleton.usa       facenose@gmail.usa

括号是可以在替换字符串中用\ 1 \ 2 \ 3 ...引用的分组。

"^"        - anchor to start of string
"(.+@)"    - take everything up to '@', we will reference this as \1
".+"       - any number of characters following the @ sign
"+(\..+$)" - everything followed by the last period, anchored to the end of the string
we will reference this with \2

"\1gmail\2" - take the first reference, follow it with "gmail", then follow that 
with the second reference

答案 2 :(得分:0)

您不需要游标,因为UPDATE就足够了(除非您遗漏的故事还有更多)。记住,逐行=慢一点!另外,您在创建过程代码中有一些语法错误。此更新仅选择与您的旧域条件匹配的行,并替换为传入的新域。

Create or replace Procedure PR_Q3(P_OldDomain varchar2, P_NewDomain Varchar2)
As
BEGIN
  UPDATE Broker
    Set Email_Address = regexp_replace(Email_Address, '^(.*@.*)'||P_OldDomain, '\1'||P_NewDomain)
    WHERE REGEXP_LIKE(Email_Address, '.*@.*'||P_OldDomain);
  COMMIT;
End PR_Q3;

别忘了添加异常处理,我会留给您。