Open In App

re.search() in Python

Last Updated : 08 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

Python
import re
s = "Hello, welcome to the world of Python."
pat = "welcome" # pattern

res = re.search(pat, s) # search pattern

if res:
    print("Yes")
else:
    print("No")

Output
Yes

Explanation: This code searches the entire string to find if the pattern "welcome" exists. If a match is found, it confirms the presence by printing "Yes" otherwise, it prints "No".

Syntax of re.search()

re.search(pattern, string, flags=0)

Parameters

  • pattern: A regex pattern to search for; can be simple text or a complex expression.
  • string: The target string where the pattern is searched.
  • flags (optional): Modifiers that change matching behavior (e.g., case-insensitive); default is 0.

Returns

  • Match object: Returned if a match is found; provides methods like .group() and .start().
  • None: Returned if no match is found.

Examples of re.search()

Example 1: In this example, we search for the first number that appears in a given string using a regular expression. It support special characters like \d for digits, \w for word characters and more, allowing us to search for complex patterns efficiently.

Python
import re

s = "I have 2 apples and 10 oranges."
pat = r"\d+"  # match digits

res = re.search(pat, s)  # search pattern

if res:
    print(res.group())
else:
    print("No")

Output
2

Explanation: This code searches for the first occurrence of one or more digits in the string. The pattern \d+ matches one or more digits and re.search() returns the first match it finds.

Example 2: In this example, we search for a phone number in a string using a regular expression pattern. A match object returned by the search provides useful methods like group() to get the matched text and start() to find the starting index of the match in the original string.

Python
import re

s = "My phone number is 123-456-7890."
pat = r"\d{3}-\d{3}-\d{4}"  # match phone number format

res = re.search(pat, s)  # search pattern

if res:
    print(res.group())  # matched number
    print(res.start())  # match start index
else:
    print("No")

Output
123-456-7890
19

Explanation: This pattern matches a phone number in the format 123-456-7890. The group() method returns the matched string and start() gives the index at which the match begins.

Example 3: In this example, we want to check whether the given string begins with a capital letter (A–Z). We're using the re.search() function along with a regular expression pattern that looks for a capital letter at the beginning of the string.

Python
import re

s = "Python is great"
pat = r"^[A-Z]"  # match capital letter at start

res = re.search(pat, s)  # search pattern

if res:
    print(res.group())
else:
    print("No")

Output
V

Explanation: This pattern ^[A-Z] checks if the string starts with an uppercase letter. If it does, group() returns that letter otherwise, "No" is printed.


Next Article
Article Tags :
Practice Tags :

Similar Reads