2017-07-28 31 views
1

我是正則表達式的新手。我正試圖從列表中刪除不屬於特定域的電子郵件。REGEXP_REPLACE替換列表中除特定域以外的電子郵件

例如,我有電子郵件的文章:

[email protected] , [email protected], [email protected], 
[email protected], [email protected] , [email protected] 

我只需要下載Gmail IDS:

[email protected], [email protected] 

請注意,我們可能有逗號分隔符前的空格。 感謝任何幫助!

+0

使用像%gmail.com作爲選擇*從Table_name其中電子郵件像%gmail.com –

+1

修復您的數據模型。不要將列表存儲在字符串中。這不是存儲列表的SQLish方式。 –

+0

使用REPLACE_REGEXP函數,您將在其中定義匹配要過濾的域的表達式並將其替換爲空字符串 –

回答

0

而不是抑制不匹配特定域(在你的榜樣,gmail.com),你可以嘗試只得到那些符合域電子郵件的電子郵件:

WITH a1 AS (
    SELECT '[email protected] , [email protected], [email protected],[email protected], [email protected] , [email protected]' AS email_list FROM dual 
) 
SELECT LISTAGG(TRIM(email), ',') WITHIN GROUP (ORDER BY priority) 
    FROM (
    SELECT REGEXP_SUBSTR(email_list, '[^,][email protected]', 1, LEVEL, 'i') AS email 
     , LEVEL AS priority 
     FROM a1 
    CONNECT BY LEVEL <= REGEXP_COUNT(email_list, '[^,][email protected]', 1, 'i') 
); 

儘管如此,甲骨文可能不是最好的工具(你是否將這些電子郵件地址作爲列表存儲在某個表的某個表中?如果是這樣,那麼@ GordonLinoff的評論是適當的 - 如果可以的話,修正你的數據模型)。

2

這可能是你的開始。

SELECT * 
    FROM ( SELECT REGEXP_SUBSTR (str, 
            '[[:alnum:]\.\+][email protected]', 
            1, 
            LEVEL) 
         AS SUBSTR 
       FROM (SELECT ' [email protected] , [email protected], [email protected],[email protected], [email protected] , [email protected], [email protected], foobar ' 
           AS str 
         FROM DUAL) 
     CONNECT BY LEVEL <= LENGTH (REGEXP_REPLACE (str, '[^,]+')) + 1) 
WHERE SUBSTR IS NOT NULL ; 

放在幾個例子,但郵件檢查均應符合相應的RFC,是一個使用一個方法來看看維基百科關於他們進一步認識https://en.wikipedia.org/wiki/Email_address

啓示https://stackoverflow.com/a/17597049/869069

0

這裏CTE只是爲了解決這個問題。第一步是創建一個包含解析列表元素的CTE「表」。然後從中選擇。 CTE正則表達式處理NULL列表元素。

with main_tbl(email) as (
    select ' [email protected] , [email protected], [email protected],[email protected], [email protected] , [email protected], [email protected], foobar ' 
    from dual 
), 
email_list(email_addr) as (
    select trim(regexp_substr(email, '(.*?)(,|$)', 1, level, NULL, 1)) 
    from main_tbl 
    connect by level <= regexp_count(email, ',')+1 
) 
-- select * from email_list; 
select LISTAGG(TRIM(email_addr), ', ') WITHIN GROUP (ORDER BY email_addr) 
from email_list 
where lower(email_addr) like '%gmail.com'; 
相關問題