JammyTone
JammyTone

Reputation: 53

Use regex to select line by line instead of every line at once?

I want to alter playlists and remove everything between and including forward slashes.

Right now I have this regex working:

\/[^\]]*\/\W*

Converts this:

/Users/jimmy/Music/iTunes/iTunes Media/Music/Godflesh/Streetcleaner/01 Like Rats.m4a

To this:

01 Like Rats.m4a

However, I want this:

#EXTM3U
#EXTINF:314,Hey Man Nice Shot - Filter
/Users/jimmy/Music/iTunes/iTunes Media/Music/Filter/Short Bus/01 Hey Man Nice Shot.mp3
#EXTINF:173,Biscuits For Smut - Helmet
/Users/jimmy/Music/iTunes/iTunes Media/Music/Helmet/Betty/03 Biscuits For Smut.mp3
#EXTINF:267,Like Rats - Godflesh
/Users/jimmy/Music/iTunes/iTunes Media/Music/Godflesh/Streetcleaner/01 Like Rats.m4a

To be filtered to end up like this:

#EXTM3U
#EXTINF:314,Hey Man Nice Shot - Filter
01 Hey Man Nice Shot.mp3
#EXTINF:173,Biscuits For Smut - Helmet
03 Biscuits For Smut.mp3
#EXTINF:267,Like Rats - Godflesh
01 Like Rats.m4a

But with:

\/[^\]]*\/\W*

I end up with this:

#EXTM3U
#EXTINF:314,Hey Man Nice Shot - Filter
01 Like Rats.m4a

How can I alter that regex to not remove everything between forward slashes in all the text at once? I just want it to do it line by line.

Here it is in action:

https://regex101.com/r/RhdWA5/13

Upvotes: 2

Views: 96

Answers (3)

Sky.Zhao
Sky.Zhao

Reputation: 31

Try this one:

\/.*\/

full regex should be like this, enabling global option:

new regExp(/\/.*\//ig)

and end up like this:

#EXTM3U
#EXTINF:314,Hey Man Nice Shot - Filter
01 Hey Man Nice Shot.mp3
#EXTINF:173,Biscuits For Smut - Helmet
03 Biscuits For Smut.mp3
#EXTINF:267,Like Rats - Godflesh
01 Like Rats.m4a

I made it an example also https://regex101.com/r/RhdWA5/17

Upvotes: 3

Allan
Allan

Reputation: 12438

I have updated your regex into:

^\/[^\n]*\/

After substitution by nothing it gives:

#EXTM3U
#EXTINF:314,Hey Man Nice Shot - Filter
01 Hey Man Nice Shot.mp3
#EXTINF:173,Biscuits For Smut - Helmet
03 Biscuits For Smut.mp3
#EXTINF:267,Like Rats - Godflesh
01 Like Rats.m4a

demo: https://regex101.com/r/RhdWA5/16

Upvotes: 5

Ildar Akhmetov
Ildar Akhmetov

Reputation: 1431

This regex should solve the problem:

[^\/]+\.[\w\d]+

It matches just the filenames.

Working example: https://regex101.com/r/FobLOT/1

Upvotes: 2

Related Questions