How to extract substring between single quotation marks in Python

2 Answers

0 votes
def extract_substring(s):
    start_pos = s.find("'")
    
    if start_pos != -1:
        end_pos = s.find("'", start_pos + 1)
        if end_pos != -1:
            return s[start_pos + 1:end_pos]
    
    return ""


s = "c# 'Programming' Language";

subs = extract_substring(s)

print("'{}'".format(subs))



'''
run:

'Programming'

'''

 



answered Feb 11, 2025 by avibootz
0 votes
import re

def extract_substring(s):
    # Regular expression pattern to find substrings between single quotation marks
    pattern = r"'(.*?)'"
    
    # Find all matches in the input text
    substrings = re.findall(pattern, s)
    
    if not substrings: 
        return ""
        
    return substrings[0]
 
 
s = "c# 'Programming' Language";
 
subs = extract_substring(s)
 
print("'{}'".format(subs))


 
'''
run:
 
'Programming'
 
'''

 



answered Feb 11, 2025 by avibootz

Related questions

1 answer 104 views
1 answer 101 views
1 answer 89 views
1 answer 117 views
1 answer 103 views
...