
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to use regular expressions (Regex) to filter valid emails in a Pandas series?
A regular expression is a sequence of characters that define a search pattern. In this program, we will use these regular expressions to filter valid and invalid emails.
We will define a Pandas series with different emails and check which email is valid. We will also use a python library called re which is used for regex purposes.
Algorithm
Step 1: Define a Pandas series of different email ids. Step 2: Define a regex for checking validity of emails. Step 3: Use the re.search() function in the re library for checking the validity of the email.
Example Code
import pandas as pd import re series = pd.Series(['jimmyadams123@gmail.com', 'hellowolrd.com']) regex = '^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$' for email in series: if re.search(regex, email): print("{}: Valid Email".format(email)) else: print("{} : Invalid Email".format(email))
Output
jimmyadams123@gmail.com: Valid Email hellowolrd.com : Invalid Email
Explanation
The regex variable has the following symbols:
- ^: Anchor for the start of the string
- [ ]: Opening and closing square brackets define a character class to match a single character
- \ : Escape character
- . : The dot matches any character except the newline symbol
- {} : The opening and closing curly brackets are used for range definition
- $ : The dollar sign is the anchor for the end of the string
- Related Articles
- How to filter rows in Pandas by regex?
- How to retrieve rows of a series object by regular expression in the pandas filter method?
- How to use regular expressions in a CSS locator?
- Regular Expressions syntax in Java Regex
- Regex quantifiers in Java Regular Expressions
- How to count the valid elements from a series object in Pandas?
- How to use regular expressions with TestNG?
- Write a program in Python to filter valid dates in a given series
- Program to check valid mobile number using Java regular expressions
- How to use regular expressions in css in Selenium with python?
- How to use regular expressions in xpath in Selenium with python?
- Checking for valid email address using regular expressions in Java
- How do you use regular expressions in Cucumber?
- How to retrieve the last valid index from a series object using pandas series.last_valid_index() method?
- Use a character class in Java Regular Expressions

Advertisements