Skip to content
Advertisement

REGEXP to identify and replace an email

long story short, I need to identify an email that may have 2 different endings:

“HREmail@companyabc.com” “HREmail@company.com”

I need to replace instances of the two above examples with:

“ITEmail@company.com”

I am mostly new to Oracle, here’s what I have so far:

CASE
    WHEN REGEXP_LIKE(EMAIL_LIST, '^HREmail@companyabc.com$', 'i') THEN REGEXP_REPLACE(EMAIL_LIST,'^HREmail@companyabc.com$', ';ITEmail@company.com;')
    WHEN NOT REGEXP_LIKE(EMAIL_LIST, '^HREmail@companyabc.com$', 'i') THEN EMAIL_LIST || ';ITEmail@company.com;'
    ELSE EMAIL_LIST
END

The email_list column that holds the emails contains emails separated by ‘;’. For instance: 'email1@company.com;email2@company.com;hremail@companyabc.com;hremail@comapny.com;' is one row from that column. I want to change that so the above row would look like this: email1@company.com;email2@company.com;ITEmail@company.com.

It’s pretty easy to just check for one of the emails, and I am aware I can just put in an OR… and add in another REGEXP_LIKE statement with the other email, but is there a way I can look for both emails in one REGEXP_LIKE statement?

Thanks

Advertisement

Answer

Yes, there is. Make the ‘abc’ optional:

HREmail@company(abc)?.com$

Don’t forget to escape the dot in regular expressions!

Question: Do you need to replace both with ITEmail@company.com? That is not what you have in your code. In your code, in the first case it’s ITEmail@companyabc.com.

If you must replace both with ITEmail@company.com, it’s trivial. For the more interesting case:

regexp_replace(<string>, '^HR(Email@company(abc)?.com)$', 'IT1')

This will replace HR with IT and then copy the first capturing group (what’s within the outer parentheses) exactly as it is in the input – with or without abc.

For example:

with
  sample_inputs(str) as (
    select 'HR@mycompany.com'       from dual union all
    select 'HREmail@company.com'    from dual union all
    select 'ITEmail@company.com'    from dual union all
    select 'HR@companyabc.com'      from dual union all
    select 'HREmail@companyabc.com' from dual union all
    select 'ACEmail@company.com'    from dual
  )
select str as old_str, 
       regexp_replace(str, '^HR(Email@company(abc)?.com)$', 'IT1')
         as new_str
from   sample_inputs
;

OLD_STR                  NEW_STR                 
------------------------ ------------------------
HR@mycompany.com         HR@mycompany.com        
HREmail@company.com      ITEmail@company.com     
ITEmail@company.com      ITEmail@company.com     
HR@companyabc.com        HR@companyabc.com       
HREmail@companyabc.com   ITEmail@companyabc.com  
ACEmail@company.com      ACEmail@company.com  

EDIT (one of many) – I saw later in your question that you need to replace in a semicolon-separated list, not the full string. So, why did you have the ^ and $ anchors in your attempt?

Anyway – you can adapt what I gave you above to a list like you have. Write back if you need help with that part (which is quite unrelated to the main question, of how to handle both “to-be-replaced” strings in a single regexp).

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement