A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/re-search-in-python/ below:

re.search() in Python - GeeksforGeeks

re.search() in Python

Last Updated : 12 May, 2025

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")

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:

Returns:

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")

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")

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")

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.

Related Articles:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4