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:

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:

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:

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:

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