用lua获取目录,文件名,扩展名

用lua获取目录,文件名,扩展名


很多时候我们需要从全路径中取得目录,文件名或者扩展名。办法有许多,看lua是怎么做的:

–获取路径
function stripfilename(filename)
return string.match(filename, “(.+)/[^/]*%.%w+$”) — *nix system
–return string.match(filename, “(.+)\\[^\\]*%.%w+$”) — windows
end

–获取文件名
function strippath(filename)
return string.match(filename, “.+/([^/]*%.%w+)$”) — *nix system
–return string.match(filename, “.+\\([^\\]*%.%w+)$”) — *nix system
end

–去除扩展名
function stripextension(filename)
local idx = filename:match(“.+()%.%w+$”)
if(idx) then
return filename:sub(1, idx-1)
else
return filename
end
end

–获取扩展名
function getextension(filename)
return filename:match(“.+%.(%w+)$”)
end

refer to: http://lua-users.org/lists/lua-l/2010-07/msg00088.html

你可能感兴趣的:(Lua)