VictorGram
VictorGram

Reputation: 2661

Oracle : REGEXP_REPLACE in a substring

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?

Upvotes: 1

Views: 1393

Answers (3)

Littlefoot
Littlefoot

Reputation: 143113

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>

Upvotes: 1

Kaushik Nayak
Kaushik Nayak

Reputation: 31726

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] |

Upvotes: 1

Kris Rice
Kris Rice

Reputation: 3410

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]

Upvotes: 2

Related Questions