开发者

preg_match_all ignore words

I try to create a regex to capture emails ending not .info/.con containing no aaa/bbb.

Is this the correct syntax?

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

To get this:

caaac@ccc.com = no
ccc@ccbbb.com = no
cccc@cccc.com = good (address syntax correct + term absent before or 开发者_JAVA技巧after the @)

Thank you for your reply.

This syntax works fine SEE HERE (thank you to STEMA) except for a string that includes spaces.

e.g:

$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);

Cordially


Simply use a positive expression and check that it did not match anything.

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

Also, there is no need to use preg_match_all if you are just interested whether a pattern matched or not.


If I understand your requirements right, then this would be the regex you can use together with @Tomalak answer.

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

This pattern matches the stuff you don't want.

.*@.*(?:aaa|bbb) matches aaa or bbb after the @

the \.(?:com|info)$ is the other part, this matches if your email address ends with .com or .info

You can see it online here on Regexr

Update:

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

This will match aaa or bbb and the string has to end with .com or .info

See it online here on Regexr


Here's the solution:

#(?<=^|\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]);
        }
}

Cordially

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜