user3922906
user3922906

Reputation: 65

Regular Expression capture filename and optional extension

Given a file path with folder name, file name and an optional file extension, I would like to capture the file name (without extension) and the extension.

For example:

"c:\temp\test.txt" -> matches = "test" and ".txt"
"c:\temp\blob"     -> matches = "blob" and ""

Thanks in advance.

Upvotes: 1

Views: 1294

Answers (1)

Erik Gillespie
Erik Gillespie

Reputation: 3959

This should match any characters, a backslash, any non-backslash and non-period characters (filename), and then an optional period and series of non-period/non-backslash characters for the extension.

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

Upvotes: 2

Related Questions