How to extract all the substrings between single quotation marks in Python

1 Answer

0 votes
import re
 
def extract_substrings(text):
    # Regular expression pattern to find substrings between single quotation marks
    pattern = r"'(.*?)'"
     
    # Find all matches in the input text
    substrings = re.findall(pattern, text)
     
    if not substrings: 
        return ""
        
    return substrings
 
 
s = "Python is a 'high-level', 'general-purpose' 'type-checked' 'garbage-collected'"
 
result = extract_substrings(s)
print(result)
 
 
 
'''
run:
 
['high-level', 'general-purpose', 'type-checked', 'garbage-collected']
 
'''

 



answered Feb 11, 2025 by avibootz
edited Feb 11, 2025 by avibootz
...