简体   繁体   中英

Regular expression to match string after exactly two forward slash

say I have a file name like this

 ./Music/pop-rock/Stray Cats/Greatest Hits/01 - Rock This Town.ogg
 ./Music/classical/Handel/Basic Handel/disk1/04 - Wassermusik - Allegro.ogg

I want a regular expression that will match

Stray Cats 

and

Handel 

since they come after exactly two forward slash.

Also will I use the idea if I wanted to match what came exactly after 3 forward slashes?

like

Greatest Hits

I have searched and haven't got any expression that is consistent all through

I wouldn't use regex. Maybe use str.split() like so:

filepath = './Music/pop-rock/Stray Cats/Greatest Hits/01 - Rock This Town.ogg'
answer = filepath.split('/')[3]

If you want to get the result after 3 slashes, just change the index of the split list.

Instead of regex, use a str.split() function. Do something like:

pathname = r'./Music/classical/Handel/Basic Handel/disk1/04 - Wassermusik - Allegro.ogg'
my_str = pathname.split('/')[3]

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