Forest
Forest

Reputation: 938

Matching up until another expression in a regular expression

I have the following series of strings. I need to only match up until a certain point in each one, leaving out the numbers after the file extension.

folder/file.ext
folder/folder/file.ext
file.ext
file.file.ext
folder/file.ext/320
folder/folder/file.ext/320
file.ext/320
file.file.ext/320
folder/file.ext/320/240
folder/folder/file.ext/320/240
file.ext/320/240
file.file.ext/320/240

In other words, I want to transform the above into:

folder/file.ext
folder/folder/file.ext
file.ext
file.file.ext
folder/file.ext
folder/folder/file.ext
file.ext
file.file.ext
folder/file.ext
folder/folder/file.ext
file.ext
file.file.ext

I was using the following expression to match the folder(s), file and extension:

[a-zA-Z0-9_/.-]+

But it accidentally matches the /000 or /000/000 that can be tacked on after the filename.

This expression gets closer, but leaves out the .ext:

(.+?)(\.[^.]*$|$)

How do I write an expression that selected the folders, file and extension, but leaves out anything after the extension?

Upvotes: 0

Views: 23

Answers (1)

Toto
Toto

Reputation: 91415

I'd do simply:

^(.+\.[^/]+) 

This will match the path + filename + extension and keep them in group 1.

It works only if there are no dots in the path after extension.

Upvotes: 1

Related Questions