Regex for extracting filename from path

Solution 1:

^\\(.+\\)*(.+)\.(.+)$

This regex has been tested on these two examples:

\var\www\www.example.com\index.php
\index.php

First block "(.+\)*" matches directory path.
Second block "(.+)" matches file name without extension.
Third block "(.+)$" matches extension.

Solution 2:

This will get the filename but will also get the dot. You might want to truncate the last digit from it in your code.

[\w-]+\.

Update

@Geoman if you have spaces in file name then use the modified pattern below

[ \w-]+\.      (space added in brackets)

Demo

Solution 3:

This is just a slight variation on @hmd's so you don't have to truncate the .

[ \w-]+?(?=\.)

Demo

Really, thanks goes to @hmd. I've only slightly improved on it.

Solution 4:

Try this:

[^\\]+(?=\.pdf$)

It matches everything except back-slash followed by .pdf at the end of the string.

You can also (and maybe it's even better) take the part you want into the capturing group like that:

([^\\]+)\.pdf$

But how you refer to this group (the part in parenthesis) depends on the language or regexp flavor you're using. In most cases it'll be smth like $1, or \1, or the library will provide some method for getting capturing group by its number after regexp match.