简体   繁体   中英

How to split a string into multiple parts?

I have f = imgString.split('medias/')[1] g = f.split('?')[0] print(g) but I'd prefer it on one line. How can I split this string into multiple parts 'media/Clearance.png?sometexthere' .Ideally I'd like just the Clearance.png. so if I was splitting it it'd be 'media/', 'Clearance.png' and '?sometexthere'

string = 'media/Clearance.png?sometexthere'
string.split("/")[1].split("?")[0]

If it is always the same format you can use regex like this one : ([a-zA-Z]*)\\/(.*)\\?([a-zA-Z]*) and then with re.group() you can have all the parts of your string :)
You can check it here link !

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