簡體   English   中英

preg_match_all 忽略單詞

[英]preg_match_all ignore words

我嘗試創建一個正則表達式來捕獲以 not.info/.con 結尾且不包含 aaa/bbb 的電子郵件。

這是正確的語法嗎?

Eg: // search email ending in .com/.info containing no aaa/bbb
preg_match_all('#((?=.*@.*(?:com|info))(!.*(?:aaa|bbb)).*)#ui', $html, $emails);

要得到這個:

caaac@ccc.com = no
ccc@ccbbb.com = no
cccc@cccc.com = good (address syntax correct + term absent before or after the @)

感謝你的回復。

除了包含空格的字符串外,此語法運行良好,請參見此處(感謝 STEMA)。

例如:

$string = "email1@address.com blah email2@aaaaess.com blah email3@address.info embbbil4@adress.com";
preg_match_all("#^(?!.*aaa)(?!.*bbb).*@.*\.(?:com|info)$#im", $string, $matches);

親切地

只需使用一個肯定的表達式並檢查它是否不匹配任何內容。

if (preg_match(...) == 0)

此外,如果您只是對模式是否匹配感興趣,則無需使用preg_match_all

如果我正確理解您的要求,那么這將是您可以與@Tomalak 答案一起使用的正則表達式。

preg_match('#.*@.*(?:aaa|bbb)|\.(?:com|info)$#ui', $html, $emails);

這種模式匹配你想要的東西。

.*@.*(?:aaa|bbb)匹配@之后的 aaa 或 bbb

\.(?:com|info)$是另一部分,如果您的 email 地址以.com.info結尾,則匹配

您可以在 Regexr 上在線查看

更新:

.*(?:aaa|bbb).*\.(?:com|info)$

這將匹配aaabbb並且字符串必須以.com.info結尾

在 Regexr 上在線查看

這是解決方案:

#(?<=^|\s)(?![\w@]*(?:aaa|bbb|(?:[0-9].*){3,}))[a-z0-9-_.]*@[a-z0-9-_.]*\.(?:com|net|org|info|biz)(?=\s|$)#im

Function:

function get_emails($str){
    preg_match_all('#(?<=^|\s)(?![\w@]*(?:aaa|bbb|(?:[0-9].*){3,}))[a-z0-9-_.]*@[a-z0-9-_.]*\.(?:com|net|org|info|biz)(?=\s|$)#im', $str, $output);
    if(is_array($output[0]) && count($output[0])>0) {
            return array_unique($output[0]);
        }
}

親切地

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM