Get file name from URL using Lua
Asked Answered
C

1

9

How do I get the file name from a URL using Lua string manipulations.

I have this url

https://thisisarandomsite.com/some_dir/src/blah/blah/7fd34a0945b036685bbd6cc2583a5c30.jpg

And I want to get the 7fd34a0945b036685bbd6cc2583a5c30.jpg, it can be a random site so the site name is not static.

Carolus answered 16/8, 2013 at 2:9 Comment(0)
S
13

Try this:

local str = "https://thisisarandomsite.com/some_dir/src/blah/blah/7fd34a0945b036685bbd6cc2583a5c30.jpg"
local name = str:match( "([^/]+)$" )

You can customise the match pattern from this guide.

Shuttlecock answered 16/8, 2013 at 2:14 Comment(1)
This pattern may be slightly simpler to understand: ".+/(.*)$".Cornwall

© 2022 - 2024 — McMap. All rights reserved.