DEV Community

Nw3965
Nw3965

Posted on

Validate email addresses using regular expressions

I realized that I can use the regular expression to validate the email address.

The code without AI.

def validEmailList(emailList):
    emailList(len(@))
Enter fullscreen mode Exit fullscreen mode

After searching and asking AI.

import re

def validEmailList(emailList):
    valid_emails = []

    for email in emailList:
        # Validate email addresses using regular expressions
        if re.match(r'^[^\s@]+@[^\s@]+\.[^\s@]+$', email):
            valid_emails.append(email)

    return valid_emails
Enter fullscreen mode Exit fullscreen mode

What is import re?

Answer;
re is the library for using regular expressions.

Note.

^: Indicates the beginning of a line.
[^\s@]+: Indicates a space or a character other than @ repeated one or more times.
@: Indicates an @ character.
. Indicates a : . Indicates a . character. However, . has a special meaning in regular expressions, so \ is used to escape it.
$: Indicates the end of a line.

Top comments (0)