Regex to get a filename from a url

This one works well for me.

(\w+)(\.\w+)+(?!.*(\w+)(\.\w+)+)

(?:.+\/)(.+)

Select all up to the last forward slash (/), capture everything after this forward slash. Use subpattern $1.


The examples above fails to get file name "file-1.name.zip" from this URL:

"http://sub.domain.com/sub/sub/handler?file=data/file-1.name.zip&v=1"

So I created my REGEX version:

[^/\\&\?]+\.\w{3,4}(?=([\?&].*$|$))

Explanation:

[^/\\&\?]+          # file name - group of chars without URL delimiters
\.\w{3,4}           # file extension - 3 or 4 word chars
(?=([\?&].*$|$))    # positive lookahead to ensure that file name is at the end of string or there is some QueryString parameters, that needs to be ignored

Tags:

Regex