-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
Introduction to Python
Python Basics
Python Syntax
Python Comments
Python Variables
Python Data Types
Python Casting
Python I/O
Python Operators
Cotrol Structures
Data Structures
Python Strings
Python Lists
Python Tuples
Python Dictionaries
Python Sets
Python Arrays
Python Bytes and Bytearray
Date and Time
Functions and Module
File Handling
Python OOP
Advanced Concepts
Python Scope
Python Modules
Python JSON
Python RegEx
Python PIP
Python Try...Except
Python String Formatting
Python User Input
Python VirtualEnv
Python Math
Python DSA
Python DSA
Lists and Arrays
Python Stacks
Python Queues
Linked Lists
Python Hash Tables
Python Trees
Python Binary Trees
Binary Search Trees
Python AVL Trees
Python Graphs
Searching Algorithms
Sorting Algorithms
RegEx stands for Regular Expression. It is a powerful tool used to search, match, and manipulate strings based on patterns.
Python provides support for regular expressions through the built-in re
module.
re
Moduleimport re
re
Function | Description |
---|---|
re.search() |
Searches for a match anywhere in string |
re.match() |
Checks for a match only at the beginning |
re.findall() |
Returns all matches in a list |
re.sub() |
Replaces matches with another string |
re.split() |
Splits string by pattern |
import re
txt = "I love Python"
result = re.search("Python", txt)
if result:
print("Match found!")
txt = "apple, banana, apple, mango"
matches = re.findall("apple", txt)
print(matches) # ['apple', 'apple']
txt = "Python is fun"
match = re.match("Python", txt)
if match:
print("Yes, it starts with Python")
txt = "I like Java"
updated = re.sub("Java", "Python", txt)
print(updated) # I like Python
txt = "one, two, three"
parts = re.split(", ", txt)
print(parts) # ['one', 'two', 'three']
Symbol | Meaning |
---|---|
. |
Any character (except newline) |
^ |
Starts with |
$ |
Ends with |
* |
Zero or more |
+ |
One or more |
? |
Zero or one |
[] |
Set of characters |
\ |
Escape character |
{} |
Specific number of occurrences |
` | ` |
() |
Group |
Use raw strings (r""
) to avoid escaping characters like \
.
pattern = r"\d+" # Matches one or more digits
Q1. Write a Python program to use re.search()
to find the word "apple"
in a sentence.
Q2. Write a Python program to use re.match()
to check if a string starts with "Hello"
.
Q3. Write a Python program to use re.findall()
to count how many times the word "dog"
appears in a sentence.
Q4. Write a Python program to replace all numbers in a string with the "#"
symbol using re.sub()
.
Q5. Write a Python program to split a string using a comma and space ", "
as the separator using re.split()
.
Q6. Write a Python program to create a regex pattern to find all email addresses in a string.
Q7. Write a Python program to match all words starting with a capital letter using regex.
Q8. Write a Python program to use ^
and $
to check if a string starts and ends with specific words.
Q9. Write a Python program to match all 3-digit numbers from a string using a regex pattern like \b\d{3}\b
.
Q10. Write a Python program to extract all words from a sentence using \w+
.
Introduction to Python
Python Basics
Python Syntax
Python Comments
Python Variables
Python Data Types
Python Casting
Python I/O
Python Operators
Cotrol Structures
Data Structures
Python Strings
Python Lists
Python Tuples
Python Dictionaries
Python Sets
Python Arrays
Python Bytes and Bytearray
Date and Time
Functions and Module
File Handling
Python OOP
Advanced Concepts
Python Scope
Python Modules
Python JSON
Python RegEx
Python PIP
Python Try...Except
Python String Formatting
Python User Input
Python VirtualEnv
Python Math
Python DSA
Python DSA
Lists and Arrays
Python Stacks
Python Queues
Linked Lists
Python Hash Tables
Python Trees
Python Binary Trees
Binary Search Trees
Python AVL Trees
Python Graphs
Searching Algorithms
Sorting Algorithms