Python Email Validation using Regex
This Snippet is coded by a user inOnline python Compiler. You can see and run this code too.
زبان: python
This Python script demonstrates how to validate email addresses using regular expressions (regex). It imports the re module, defines a list of sample email addresses, and loops through each one to check its validity.
The regex pattern used is:
^[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:[a-zA-Z0-9-]+\.[a-zA-Z0-9]+)+$This pattern checks for a valid local part, an @ symbol, and a domain part with at least one dot. If a match is found, the email is considered valid and printed in green; otherwise, it is invalid and printed in red using ANSI color codes.
How to use:
- Copy the code into a Python file.
- Run it with Python 3.
- The output will clearly show which emails are valid and which are not.
Note: This is a basic validation script. For production use, consider more comprehensive validation techniques.