Python RegEx
We define a pattern using a regular expression to match email addresses. The pattern r”\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b” is a common pattern for matching email addresses. Using the re.search() function, the pattern is then found in the given text. If a match is found, we use the match object’s group() method to extract and print the matched email. Otherwise, a message indicating that no email was found is displayed.
import re
# Text to search
text = "Hello, my email is example@example.com"
# Define a pattern to match email addresses
pattern = r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b"
# Search for the pattern in the text
match = re.search(pattern, text)
# Check if a match is found
if match:
email = match.group()
print("Found email:", email)
else:
print("No email found.")
Output:
Found email: example@example.com
MetaCharacters are helpful, significant, and will be used in module re functions, which helps us comprehend the analogy with RE. The list of metacharacters is shown below.
- \ sed to drop the special meaning of character following it.
- [] Represent a character class.
- ^ Matches the beginning.
- $ Matches the end.
- . Matches any character except newline.
- | Means OR (Matches with any of the characters separated by it.
- ? Matches zero or one occurrence.
- * Any number of occurrences (including 0 occurrences).
- + One or more occurrences.
- {} Indicate the number of occurrences of a preceding RegEx to match.
- () Enclose a group of RegEx.
For More RegEx example, refer to Python RegEx.
Python Cheat sheet (2024)
Python is one of the most widely-used and popular programming languages, was developed by Guido van Rossum and released first in 1991. Python is a free and open-source language with a very simple and clean syntax which makes it easy for developers to learn Python. It supports object-oriented programming and is most commonly used to perform general-purpose programming. Python is used in several domains like Data Science, Machine Learning, Deep Learning, Artificial Intelligence, Scientific Computing Scripting, Networking, Game Development Web Development, Web Scraping, and various other domains.
To give a comprehensive overview of Python programming, we made a Python Cheat Sheet for Python programmers. In this Cheat Sheet of Python, you’ll learn all the basic to advanced topics and concepts of Python, like Python data types, Python for loop, Python slice, python map function, python dictionary, Python File Handling, etc.
Python Cheat sheet
- What is Python?
- Python Input
- Operators in Python
- Python Comment
- DataType in Python
- Python String
- Conditional Statements
- Python Functions
- Python BuildIn Function
- Python RegEx
- File Handling in Python
- Try and Except Statement
- Python OOPs Concepts
- Python Modules
- Python Interview Questions Answers
- Python Cheat Sheet – FAQs
Contact Us