Python Extract Substring Using Regex
Last Updated :
23 Feb, 2024
Python provides a powerful and flexible module called re
for working with regular expressions. Regular expressions (regex) are a sequence of characters that define a search pattern, and they can be incredibly useful for extracting substrings from strings. In this article, we'll explore four simple and commonly used methods to extract substrings using regex in Python.
Python Extract Substring Using Regex
Below, are the methods of Python Extract Substring Using Regex in Python.
Python Extract Substring Using Regex Using re.search()
In this example, below Python code utilizes `re.search()` to find the substring "Python" in the string "I love Python." If a match is found, it prints the matched substring using `match.group()`, demonstrating a basic example of substring extraction with regular expressions.
Python3
import re
string = "I love Python"
pattern = "Python"
match = re.search(pattern, string)
if match:
print(match.group())
Python Extract Substring Using Regex Using re.findall() Method
In this example, below Python code uses `re.findall()` to find all occurrences of the case-sensitive pattern "the" in the given string "The quick brown fox jumps over the lazy dog," and it prints the list of matching substrings.
Python3
import re
string = "The quick brown fox jumps over the lazy dog"
pattern = "the"
matches = re.findall(pattern, string)
print(matches)
Python Extract Substring Using Regex Using re.split() Method
In this example, below Python code uses `re.split()` with the pattern "," to split the string "1,2,3,4,5" into a list of substrings based on the commas. The result, when printed using `print(numbers)`, is a list containing individual numbers: ['1', '2', '3', '4', '5'].
Python3
import re
string = "1,2,3,4,5"
pattern = ","
numbers = re.split(pattern, string)
print(numbers)
Output['1', '2', '3', '4', '5']
Similar Reads
Python - Extract K length substrings The task is to extract all possible substrings of a specific length, k. This problem involves identifying and retrieving those substrings in an efficient way. Let's explore various methods to extract substrings of length k from a given string in PythonUsing List Comprehension List comprehension is t
2 min read
Python - Extract string between two substrings The problem is to extract the portion of a string that lies between two specified substrings. For example, in the string "Hello [World]!", if the substrings are "[" and "]", the goal is to extract "World".If the starting or ending substring is missing, handle the case appropriately (e.g., return an
3 min read
Python - Remove after substring in String Removing everything after a specific substring in a string involves locating the substring and then extracting only the part of the string that precedes it. For example we are given a string s="Hello, this is a sample string" we need to remove the part of string after a particular substring includin
3 min read
Python - Remove substring list from String Our task is to remove multiple substrings from a string in Python using various methods like string replace in a loop, regular expressions, list comprehensions, functools.reduce, and custom loops. For example, given the string "Hello world!" and substrings ["Hello", "ld"], we want to get " wor!" by
3 min read
Python - Test substring order Given two strings, check if substring characters occur in correct order in string. Input : test_str = 'geeksforgeeks', K = 'sees' Output : True Explanation : "s" after that "ee" and then "s" is present in order in string 1. Input : test_str = 'geeksforgeeks', K = 'seef' Output : False Explanation :
4 min read