Matt
Matt

Reputation: 3680

Regex for filename if known extension

I have come up with a regex to find me all the image within a project.

(\/|\\)(.*)\.(gif|png|jpg|jpeg)

What I am trying to do now, is find the extension (gif|jpg|etc...) and then capture the word preceeding that match (ie the filename).

I'm totally stuck on how to structure a lookbehind capture group(??)

What I am doing is looking through a large web project finding all the images that have been actually used in code, and then I'm going to clean up the image directory for ones that are no longer referenced.

Upvotes: 1

Views: 125

Answers (1)

Toto
Toto

Reputation: 91518

With Npp, you could search:

([^\/\\]+)(?=\.(gif|png|jpg|jpeg))

or

([^\/\\]+)(?=\.(?:gif|png|jpe?g))

to match the filename.

According to comment, you can do the find/replace with:

  • Ctrl+H
  • Find what: ^.*?([^\/\\]+\.(?:gif|png|jpe?g)).*?$
  • Replace with: $1
  • Replace all

This will remove everything in a line except the filename+extension.

Upvotes: 2

Related Questions