tags:

views:

77

answers:

3

Hi,

We have followed the regex for validating RFC 2822 standard.

The function is as follows:

int isValidEmail(const char *email_id)
{
    /* Regular expression to validate email */
    const char *reg_exp =
        "[a-z0-9!#$%&'*+-/=?^_`{|}~]+(\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@([a-z0-9]([a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9]([a-z0-9-]*[a-z0-9])?$";

    regex_t preg;

    /* Compile the regular expression */
    if (regcomp(&preg, reg_exp, REG_EXTENDED|REG_NOSUB|REG_ICASE) != 0)
        return FAILURE;

    /* Execute the regex, 0 for success */
    if (regexec(&preg, email_id, (size_t)0, NULL, 0) != 0)
        return FAILURE;

    regfree(&preg);

    return SUCCESS;

} 

But if I provide emails like "test@[email protected]", the emails are getting accepted.

Is there any problem with this regex?

Appreciate the help in advance.

Thanks, Mathew Liju

+6  A: 

Add a ^ at the beginning of the regex. It matches the beginning of line. Without ^ regex will match the substring [email protected] as it is a valid email id matching the regex.

Amarghosh
A: 

Try adding a start anchor ^ as:

"^[a-z0-9!#$%&'*+-/=?^_`{|}~]+(\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@([a-z0-9]([a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9]([a-z0-9-]*[a-z0-9])?$";
codaddict
+1  A: 

Your first character class contains the sequence +-/, which is treated as a range. In addition to those three characters, the range will match a period or a comma, which I'm pretty sure you don't want. You should move the hyphen to the end of the class, like it is in all the other classes.

That's the only structural problem I see with the regex. As for whether it's the right regex for an email address, well, there's no such thing. There are huge variations depending on the context and what you're doing with the regex. When it comes to email-address regexes, you're always flying seat-of-the-pants. :P

Alan Moore