779 posts
Location
Kerbin
Posted 16 May 2014 - 05:47 PM
Help me with file formats, how can I detect for example .bmp ending filename?
Other can I do, but the endings are problems.
Sorry for bad english
1610 posts
Posted 16 May 2014 - 08:20 PM
Assuming you already have the filename:
function getExtension(filename)
if filename:find("$.+(%..+)^") then
local a,b,c = filename:find("$.+(%..+)^")
return c
end
end
779 posts
Location
Kerbin
Posted 16 May 2014 - 08:58 PM
Sorry, I don't understand anything.
Please tell me what line what does.
571 posts
Location
Some Fish Bowl in Ohio.
Posted 16 May 2014 - 08:59 PM
Sorry, I don't understand anything.
Please tell me what line what does.
It detects everything after a period in a file name.
779 posts
Location
Kerbin
Posted 16 May 2014 - 09:01 PM
And why $.+(%..+)
.?
570 posts
Posted 16 May 2014 - 09:02 PM
And why $.+(%..+)
.?
It's a
pattern.
779 posts
Location
Kerbin
Posted 16 May 2014 - 09:05 PM
Uh
if IDon'tUnderstandAnything() then
print("go away")
else
print("stay here")
end
Output:
go away
1610 posts
Posted 16 May 2014 - 09:42 PM
Alright. I'll break it down:
The ": starts a string, as patterns are strings.
The $: means that the string must START with the following pattern
The .: matches any character
The +: allows the previous case to be repeated (match any characters.
The %.: matches for a REAL period
The (: starts a capture (an additional return value if the containing case was matched)
The .+ again, matches all characters
The ): closes the capture
The ^: means the string must END with this pattern.
In short it looks for something like this:
(Any text).(any text) and returns the second set of parenthasees's contents.
570 posts
Posted 16 May 2014 - 09:56 PM
The $: means that the string must START with the following pattern.
The ^: means the string must END with this pattern.
Are you sure it's not the other way around? Because your code only seems to work for me when I swap the two.
Your code could also be shortened to a one-liner:
function getExtension(filename)
return filename:match("^.+(%..+)$")
end
779 posts
Location
Kerbin
Posted 17 May 2014 - 06:56 AM
And what is filename:find
3057 posts
Location
United States of America
Posted 17 May 2014 - 02:10 PM
151 posts
Location
Earth
Posted 18 May 2014 - 12:45 AM
You need to stop complaining about our answers and read up on lua.