简体   繁体   中英

Slice string after period and before next space

I am looping through SQL statements (strings) that look like this...

'SELECT COL_1, COL_2, COL_3 from SCHEMA_1.TABLE_ABC MINUS SELECT COL_1, COL_2, COL_3 from SCHEMA_2.TABLE_WXYZ..'

Each time I loop through, I want to capture the first table name and store it. So anything after the first period (.) and before the next space. Desired output from above example:

TABLE_ABC

Any ideas?

You might be able to use re.findall here:

inp = 'SELECT COL_1, COL_2, COL_3 from SCHEMA_1.TABLE_ABC MINUS SELECT COL_1, COL_2, COL_3 from SCHEMA_2.TABLE_WXYZ..'
table_name = re.findall(r'\bfrom\s+[^.]+\.(\S+)', inp, flags=re.IGNORECASE)[0]
print(table_name)

This prints:

TABLE_ABC

You can do it in pure Python without importing any modules:

query = 'SELECT COL_1, COL_2, COL_3 from SCHEMA_1.TABLE_ABC MINUS SELECT COL_1, COL_2, COL_3 from SCHEMA_2.TABLE_WXYZ..'    
print(query.split('.', 1)[1].split(' ', 1)[0])
# TABLE_ABC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM