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:
- 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")
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:
Similar Reads
re.subn() in Python re.subn() method in Python is used to search for a pattern in a string and replace it with a new substring. It not only performs the replacement but also tells us how many times the replacement was made. We can use this method when we need to replace patterns or regular expressions in text and get a
3 min read
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
Python Regex: re.search() VS re.findall() 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. Understa
3 min read
Python - API.saved_searches() in Tweepy Twitter is a popular social network where users share messages called tweets. Twitter allows us to mine the data of any user using Twitter API or Tweepy. The data will be tweets extracted from the user. The first thing to do is get the consumer key, consumer secret, access key and access secret from
2 min read
re.match() in Python re.match method in Python is used to check if a given pattern matches the beginning of a string. Itâs like searching for a word or pattern at the start of a sentence. For example, we can use re.match to check if a string starts with a certain word, number, or symbol. We can do pattern matching using
2 min read
Python RegEx In this tutorial, you'll learn about RegEx and understand various regular expressions. Regular ExpressionsWhy Regular ExpressionsBasic Regular ExpressionsMore Regular ExpressionsCompiled Regular Expressions A RegEx is a powerful tool for matching text, based on a pre-defined pattern. It can detect t
9 min read
File Searching using Python There may be many instances when you want to search a system.Suppose while writing an mp3 player you may want to have all the '.mp3' files present. Well here's how to do it in a simple way. This code searches all the folders in the file it's being run. If you want some other kinds of files just chan
2 min read
Python - API.get_saved_search() in Tweepy Twitter is a popular social network where users share messages called tweets. Twitter allows us to mine the data of any user using Twitter API or Tweepy. The data will be tweets extracted from the user. The first thing to do is get the consumer key, consumer secret, access key and access secret from
2 min read
Python IMDbPY â Searching a person IMDbPY is a Python package which is used to retrieve and manage the data of the IMDb. In this article we will see how we can search a person with the given name in the IMDb data base, we can do to this with the help of search_person method.  Syntax : imdb_object.search_person(name)Argument : It tak
2 min read
Python - SavedSearch object in Tweepy Twitter is a popular social network where users share messages called tweets. Twitter allows us to mine the data of any user using Twitter API or Tweepy. The data will be tweets extracted from the user. The first thing to do is get the consumer key, consumer secret, access key and access secret from
2 min read