1

I need to replace some of characters from email addresses. For example :

If my email address is : [email protected] , the output should be :

[email protected] <-- removed '.' before '@mydomain.com'

Again if my email address is [email protected], the output should be :

[email protected] <-- removed '.' and '+' before '@mydomain.com'

Hence, expecting some of the chars ( like '.','+' etc ) before @mydomain.com should be removed. While I am seeing that I can use REGEXP_REPLACE to replace those chars, getting hard time to understand how to include the constraint that the replacement has to be in the substring before '@'.

May I get any help?

3 Answers 3

2

What you are looking for , I think, is this How to match "anything up until this sequence of characters" in a regular expression?

Which Oracle doesn't implement in the db ( as far as I can tell )

So that means splicing it apart based on the @ , removing the .|+ from the first part, then concatenating back together.

SQL> with q as ( select '[email protected]' 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                       
--------------------------------
[email protected]
Sign up to request clarification or add additional context in comments.

2 Comments

Should have said the full doc for regex_replace is here for more robust explanations of the parameters : docs.oracle.com/database/121/SQLRF/functions163.htm#SQLRF06302
but this replaces the '.' from @mydomain.com as well and that is not expected. result should be [email protected] (those characters should be replaced before the part @mydomain.com)
1

You may split the domain and name and then apply REGEXP_REPLACE or TRANSLATE on name.

Another option is to use INSTR and SUBSTR

SQL Fiddle

Oracle 11g R2 Schema Setup:

create table yourtable as
select '[email protected]' emailid FROM DUAL UNION ALL
select '[email protected]' FROM DUAL UNION ALL
select '[email protected]' FROM DUAL

Query 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 |
|----------------------------------|
|          [email protected] |
|          [email protected] |
| [email protected] |

Query 2:

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

Results:

|                          EMAILID |
|----------------------------------|
|          [email protected] |
|          [email protected] |
| [email protected] |

Comments

1

If I understood you correctly, you want to leave only letters (possibly digits?) in the first part of the e-mail address. If that's so, how about this?

In REGEXP_REPLACE, the \W represents a non-word, so - anything that isn't a letter or a digit or an underscore (as it is considered to be a word) is being removed. In order to remove the underscore as well, |_ is added so - finally - it is read as remove non-words or underlines.

SQL> with test (email) as (
  2    select '[email protected]'            from dual union all
  3    select '[email protected]'           from dual union all
  4    select '[email protected]' from dual union all
  5    select '[email protected]' from dual union all
  6    select 'pat_ty#b.e?a!d*[email protected]'       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
---------------------------------- ----------------------------------------
[email protected]            [email protected]
[email protected]           [email protected]
[email protected] [email protected]
[email protected] [email protected]
pat_ty#b.e?a!d*[email protected]      [email protected]

SQL>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.