Python-How to extract multiple words between two strings-(Extracting word between {})

Here we will see how to extract string between two specific character/string. This is a use case when you want to create SQL dynamically or when you want to replace word between two strings.

Use case : I want to get the words between { } curly brackets

#val_sql = "select * from {abc.table} where {cust_id} = '10000' and {dept} = 91"
###Source Code###
def extract_words_in_between(s):
    parm_list = []
    while (s.find('{')) != -1:
        sql_len = len(s)
        start_p = s.find('{')+1
        end_p   = s.find('}')
        parm = s[start_p:end_p]
        parm_list.append(parm)
        end_p = end_p+1
        s = s[end_p:]
    return parm_list

val_sql = "select * from {abc.table} where {cust_id} = '10000' and {dept} = 91"
print(extract_words_in_between(val_sql))

#Result
['abc.table', 'cust_id', 'dept']
Author: user

Leave a Reply