

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- How to filter rows in Pandas by regex?
- Regular Expressions syntax in Java Regex
- Regex quantifiers in Java Regular Expressions
- How to use regular expressions in a CSS locator?
- How to use regular expressions with TestNG?
- How to retrieve rows of a series object by regular expression in the pandas filter method?
- Program to check valid mobile number using Java regular expressions
- Write a program in Python to filter valid dates in a given series
- How to count the valid elements from a series object in Pandas?
- Use a character class in Java Regular Expressions
- Checking for valid email address using regular expressions in Java
- How do you use regular expressions in Cucumber?
- How to use regular expressions in css in Selenium with python?
- How to use regular expressions in xpath in Selenium with python?
- Use the ? quantifier in Java Regular Expressions
Advertisements