简体   繁体   中英

Regular Expression Regex function to get Email does not match capital letters

I am using regex function to get the email address from the string. The email address is inside the string covered both side with different characters like.

string=:'Here is the email address I would like to get out of here Ahmad_khalid@yahoo.com in 1 st try'

This following syntax returns email address but miss the Capital characters and returns email address from about string like [hmad_khalid@yahoo.com] capital A is missed.

$regex='`([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})`';

I didn't check your regex in detail, but if the capital characters are the only problem, then just add the i modifier after the last regex delimiter

$regex='`([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})`i';

this modifier makes the regex match case insensitive.

OK, then you should add at least some anchors \\b to the regex, to avoid partial matches.

$regex='`\b([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,6})\b`i';

See it here on Regexr

The \\b matches on a change from a word character ( A-Za-z0-9_ ) to a non-word character (all the others) and the other way round.

You should think about changing the last quantifier from {2,4} to {2,6} , there are some rare domains that are longer than 4 characters.

in javascript

function extractEmail()
{
    var sentnce="Here is the email address I would like to get out of here w555@yahoo.co.in in 1 st try";
    var regEmail=/^[a-zA-Z0-9]+[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+[\.]{1}[a-zA-Z]{2,4}$/;

var strArray=sentnce.split(' ');



    for(i=0;i<strArray.length;i++)
    {

        if(regEmail.test(strArray[i]))
        {
            alert('email: '+strArray[i]);

        }

    }

}

in your regex, _a-z0-9 doesn't include capital letters. where this occurs, use this instead: \\w ("word character" metasequence, alphanumerics or 'letters, numbers, and underscores'): [\\w-] . (edit) I'm also pretty sure you can do without the unquantified paren groups (you are matching the entire address not parsing it with match groups right, so they aren't doing anything useful), and get a somewhat prettier regex:

$regex = "\b[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)*\.[a-zA-Z]{2,6}\b"; 

vs

$regex='`\b([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,6})\b`i';

regexr link here (thanks, cool site stema :])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM