Using regular expressions - documentation for further reference

import re

text = 'gfgfdAAA1234ZZZuijjk'

m = re.search('AAA(.+?)ZZZ', text)
if m:
    found = m.group(1)

# found: 1234

or:

import re

text = 'gfgfdAAA1234ZZZuijjk'

try:
    found = re.search('AAA(.+?)ZZZ', text).group(1)
except AttributeError:
    # AAA, ZZZ not found in the original string
    found = '' # apply your error handling

# found: 1234
Answer from eumiro on Stack Overflow
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Extract a Substring from a String in Python (Position, Regex) | note.nkmk.me
April 29, 2025 - Since backslashes \ are used in ... returns a match object. You can retrieve the matched substring using the group() method of the match object....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-extract-substring-using-regex
Python Extract Substring Using Regex - GeeksforGeeks
July 23, 2025 - Below, are the methods of Python Extract Substring Using Regex in Python. ... 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.
Discussions

Python: best way to find and extract substring from a string?
Your post is kind of messed up because you didn't format your example properly as a code block. But based on the way your Markdown formatting came out, I'm guessing that the three strings you're looking for are on lines by themselves that start with a # symbol. In that case you could just do something along the lines of: lines = string.split('\n') heading_lines = [l for l in lines if l.startswith('#')] title, description, results = [l.strip('#').strip() for l in heading_lines[:3]] More on reddit.com
๐ŸŒ r/learnprogramming
10
6
October 9, 2024
The simplest way to extract a substring from a string
I personally would either use string slicing or a regex. For example, the slicing version would probably look something like substring = string[string.find("(") + 1:string.find(")")] and the regex you would use would probably look something like this: re.search(r'\((.*?)\)', string).group(1) More on reddit.com
๐ŸŒ r/learnpython
5
1
May 27, 2021
regex to extract a substring from a string in python - Stack Overflow
How do we get the following substring from a string using re in python. string1 = "fgdshdfgsLooking: 3j #123" substring = "Looking: 3j #123" string2 = "Looking: avb456j # More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Using regex to extract substrings - Stack Overflow
I have a string: s = r'"url" : "a", "meta": "b", "url" : "c"' What I want is to capture the substring url: ... up to the ,, so the expec... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
EyeHunts
tutorial.eyehunts.com โ€บ home โ€บ python regex extract substring | example code
Python regex extract substring | Example code - EyeHunts
August 24, 2021 - Call re.findall(rโ€pattern (.*)โ€, string) with pattern as the exact sequence to match within string. import re text = "Chennai is a beautiful city.Chennai is the capital of the state of Tamil Nadu" city = re.findall("Chennai", text) print(city) ...
๐ŸŒ
ReqBin
reqbin.com โ€บ code โ€บ python โ€บ h73zla88 โ€บ python-substring-example
How do I get a substring from a string in Python?
December 25, 2022 - Regular expressions are a powerful string manipulation tool in Python that is natively supported in Python. To extract a substring from a string using regular expressions, you must first import the "re" module into your code.
๐ŸŒ
Linux Hint
linuxhint.com โ€บ extract-substring-regex-python
Linux Hint โ€“ Linux Hint
March 20, 2023 - Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ python โ€บ extract a substring from a string in python
Extract a substring from a string in Python
2 weeks ago - Extract substrings from Python strings using slice notation with [start:end] indexes, or use re.search() with regex patterns for matching specific formats
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ the simplest way to extract a substring from a string
r/learnpython on Reddit: The simplest way to extract a substring from a string
May 27, 2021 -

Hello,

I'm learning python from scratch, and I am trying to figure out how to "extract" a substring from a string. So, for instance, let's say I want to extract the word within the parenthesis for each of these three strings.

'xx(hi)xx' 'abc(there)xyz' 'xx(c)xx'

I know I could use slicing, but this would involve three operations, I think. I'm wondering if there's a sort of pattern I can leverage. How do you guys approach this kind of problem?

Thank you in advance for your help. It is most appreciated!

๐ŸŒ
YouTube
youtube.com โ€บ datadaft
Python Regex: How Find a Substring in a String - YouTube
โ†“ Code Available Below! โ†“ This video shows how to find and extract substrings within python strings using the regular expressions package. Locating and extra...
Published ย  September 30, 2020
Views ย  451
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ python-substring-quick-reference
Python Substring Quick Reference
January 31, 2024 - This code extracts the first five characters from the string, forming the substring โ€˜Helloโ€™. ... The โ€œ2โ€ in the step above means it will increment forward by 2 characters each time it extracts a character โ€” in effect, skipping every other character. Letโ€™s explore a few more examples of substring extraction in different scenarios. string = "Hello, Python!" substring = string[:5] print(substring) # Outputs: Hello
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ How-to-extract-a-substring-from-inside-a-string-in-Python
How to extract a substring from inside a string in Python?
May 20, 2025 - The re module provides powerful pattern matching capabilities for complex substring extraction using regular expressions. Here we extract a price pattern from a string using re.search() ?
๐ŸŒ
EyeHunts
tutorial.eyehunts.com โ€บ home โ€บ python extract substring from string
Python extract substring from string - Tutorial - By EyeHunts
July 28, 2023 - 3. Using Regular Expressions (Regex): Regular expressions provide a powerful way to extract substrings based on more complex patterns. import re # Example string text = "Hello, World!" # Using regex to extract all capitalized words matches = re.findall(r'\b[A-Z][a-z]*\b', text) print(matches) # Output: ['Hello', 'World'] Output: These are some of the methods available in Python to extract substrings from a string.
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ python โ€บ extract substring from a string in python
How to Extract Substring From a String in Python | Delft Stack
February 2, 2024 - Sometimes, while writing programs, we have to access sub-parts of a string. These sub-parts are more commonly known as substrings. A substring is a subset of a string. In Python, we can easily do this task using string slicing or using regular expression or regex.
๐ŸŒ
Medium
medium.com โ€บ quantrium-tech โ€บ extracting-words-from-a-string-in-python-using-regex-dac4b385c1b8
Extracting Words from a string in Python using RegEx
October 6, 2020 - This is one of the ways in which you can use the () operator to extract particular patterns that we are interested in, which occur along with some other pattern that we are not interested in capturing, like we want to ignore the '@' symbol in our case. To understand all the fundamental components of regex in Python, the best way to do so is by heading to the official documentation of Python 3.8 RegEx here:
๐ŸŒ
Medium
medium.com โ€บ @blogshub4 โ€บ extracting-substrings-from-strings-in-python-f714960b8c19
Extracting Substrings from Strings in Python | by Blogshub | Medium
December 24, 2024 - Regular expressions provide a powerful way to extract substrings based on patterns. Pythonโ€™s built-in re module makes this functionality accessible. How to Use Regex for Substrings: Import the re module: Start by importing the re module in your Python script. Use re.search(): This method searches for a pattern within a string and returns a match object.