Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python Regex: re.search() VS re.findall()
Next article icon

Python Regex: re.search() VS re.findall()

Last Updated : 28 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Python’s re module provides powerful tools to search, match and manipulate text using regular expressions. Two commonly used functions are re.search(), which finds the first occurrence of a pattern in a string and re.findall(), which retrieves all matches of a pattern throughout the string. Understanding how each works will help you effectively handle text processing tasks.

What is re.search() ?

re.search() function scans through a string, looking for the first location where the regular expression pattern produces a match. It returns a match object if a match is found, otherwise returns None.

Example:

Python
import re s = "My favorite fruits are apple, banana, and mango." res = re.search(r'\b\w*a\b', s)  if res:     print(res.group()) else:     print("Not found.") 

Output
banana 

Explanation:

  • re.search(r'\b\w*a\b', s) looks for the first word in the string that ends with the letter 'a'.
  • pattern \b\w*a\b means the word must start and end at a word boundary (\b), contain any number of word characters (\w*) and end specifically with 'a'.

What is re.findall() ?

re.findall() function returns all non-overlapping matches of a pattern in a string as a list of strings. If the pattern has capturing groups, it returns a list of tuples.

Python
import re s = "My favorite fruits are apple, banana, and mango."  res = re.findall(r'\b\w*a\b', s) print(res) 

Output
['banana'] 

Explanation:

  • re.findall(r'\b\w*a\b', s) finds all words in the string that end with the letter 'a'.
  • pattern \b\w*a\b means the word must start and end at a word boundary (\b), contain any number of word characters (\w*) and end specifically with 'a'.

Example with capturing Groups

Capturing groups in regular expressions are used to extract specific parts of the matched text. You create a group by placing part of the pattern inside parentheses ().

Example 1: In this example, we want to extract the price (just the number) after the word Price by using re.search().

Python
import re  s = "Price: $30, Discount: $5" res = re.search(r'Price: \$(\d+)', s) if res:     print(res.group(1)) 

Output
30 

Explanation:

  • re.search(r'Price: \$(\d+)', s) looks for the first occurrence of the word "Price:" followed by a dollar sign $ and then one or more digits.
  • pattern Price: \$(\d+) uses parentheses () to create a capturing group that extracts just the number after "Price: $".

Example 2: Now, suppose you want to extract all numbers that come after a dollar sign ($) in the sentence. You can use re.findall() for this.

Python
import re s = "Price: $30, Discount: $5"  res = re.findall(r'\$(\d+)', s) print(res) 

Output
['30', '5'] 

Explanation:

  • re.findall(r'\$(\d+)', s) searches the entire string for all numbers that come after a dollar sign $.
  • pattern \$(\d+) uses a capturing group (\d+) to extract one or more digits following each $ symbol.

Difference between research() and re.findall()

Both functions are used to find patterns in text, but they serve different purposes depending on whether you need a single match or all matches. The table below highlights the key differences to help you choose the right function for your task.

Feature

re.search()

re.findall()

Return Type

Returns a Match object or None

Returns a list of matching strings or tuples

Match Count

Returns only the first match

Returns all non-overlapping matches

Use Case

Best for checking if a pattern exists

Best for extracting multiple matches

Access Groups

Allows access to individual groups using .group()

Capturing groups are returned as tuples

Performance

Stops at first match (faster in some cases)

Scans the entire string


Next Article
Python Regex: re.search() VS re.findall()

N

nikhilaggarwal3
Improve
Article Tags :
  • Python
  • python-regex
Practice Tags :
  • python

Similar Reads

    Python | re.search() vs re.match()
    When working with regular expressions (regex) in Python, re.search() and re.match() are two commonly used methods for pattern matching. Both are part of the re module but function differently. The key difference is that re.match() checks for a match only at the beginning of the string, while re.sear
    3 min read
    re.search() in Python
    re.search() method in Python helps to find patterns in strings. It scans through the entire string and returns the first match it finds. This method is part of Python's re-module, which allows us to work with regular expressions (regex) simply. Example:Pythonimport re s = "Hello, welcome to the worl
    3 min read
    re.findall() in Python
    re.findall() method in Python helps us find all pattern occurrences in a string. It's like searching through a sentence to find every word that matches a specific rule. We can do this using regular expressions (regex) to create the pattern and then use re.findall() to get a list of matches.Let's say
    2 min read
    Regex Cheat Sheet - Python
    Regex or Regular Expressions are an important part of Python Programming or any other Programming Language. It is used for searching and even replacing the specified text pattern. In the regular expression, a set of characters together form the search pattern. It is also known as the reg-ex pattern.
    9 min read
    re.MatchObject.span() Method in Python - regex
    re.MatchObject.span() method returns a tuple containing starting and ending index of the matched string. If group did not contribute to the match it returns(-1,-1). Syntax: re.MatchObject.span() Parameters: group (optional) By default this is 0. Return: A tuple containing starting and ending index o
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences