Python RegEX


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.


🔹 Import the re Module

import re

🔹 Basic Functions in 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

🔹 Example: Search

import re

txt = "I love Python"
result = re.search("Python", txt)

if result:
    print("Match found!")

🔹 Example: Find All Matches

txt = "apple, banana, apple, mango"
matches = re.findall("apple", txt)

print(matches)  # ['apple', 'apple']

🔹 Example: Match from Beginning

txt = "Python is fun"
match = re.match("Python", txt)

if match:
    print("Yes, it starts with Python")

🔹 Example: Replace Text

txt = "I like Java"
updated = re.sub("Java", "Python", txt)

print(updated)  # I like Python

🔹 Example: Split a String

txt = "one, two, three"
parts = re.split(", ", txt)

print(parts)  # ['one', 'two', 'three']

🔹 RegEx Metacharacters

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

🔹 Raw Strings

Use raw strings (r"") to avoid escaping characters like \.

pattern = r"\d+"  # Matches one or more digits

Practice Questions

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+.


Python RegEX Quiz

Q1: What does RegEx stand for?

A. Regular Exit
B. Regular Expression
C. Regex Engine
D. Read Expression

Q2: Which module provides regex support in Python?

A. regexpy
B. pcre
C. re
D. string

Q3: Which function checks for a match anywhere in the string?

A. match()
B. test()
C. find()
D. search()

Q4: What does \d represent in regex?

A. Any character
B. A digit
C. A delimiter
D. A date

Q5: What is the output of re.findall("a", "banana")?

A. ['a', 'a', 'a']
B. ['banana']
C. ['n', 'n']
D. []

Q6: What symbol matches any character except newline?

A. ?
B. .
C. ^
D. |

Q7: What is the purpose of re.sub()?

A. Search only
B. Delete characters
C. Replace text
D. Split string

Q8: Which regex pattern matches one or more characters?

A. ?
B. *
C. +
D. {}

Q9: Which method is used to split a string using regex?

A. split()
B. re.split()
C. re.divide()
D. re.chop()

Q10: What does [] do in regex?

A. Match spaces
B. Define a group
C. Match a set of characters
D. Ignore characters

Go Back Top