What is the neatest way to split out a Path Name into its components in Lua

别来无恙 提交于 2019-12-04 22:47:04
> return string.match([[c:\temp\test\myfile.txt]], "(.-)([^\\]-([^%.]+))$")
c:\temp\test\   myfile.txt  txt

This seems to do exactly what you want.

Here is an improved version that works for Windows and Unix paths and also handles files without dots (or files with multiple dots):

= string.match([[/mnt/tmp/myfile.txt]], "(.-)([^\\/]-%.?([^%.\\/]*))$")
"/mnt/tmp/" "myfile.txt"    "txt"

= string.match([[/mnt/tmp/myfile.txt.1]], "(.-)([^\\/]-%.?([^%.\\/]*))$")
"/mnt/tmp/" "myfile.txt.1"  "1"

= string.match([[c:\temp\test\myfile.txt]], "(.-)([^\\/]-%.?([^%.\\/]*))$")
"c:\\temp\\test\\"  "myfile.txt"    "txt"

= string.match([[/test.i/directory.here/filename]], "(.-)([^\\/]-%.?([^%.\\/]*))$")
"/test.i/directory.here/"   "filename"  "filename"
Col_Blimp

Split string in Lua?

There is a few string to table functions there, split "\" as \ cant be in a folder name anyway so you'll end up with a table with index one being the drive and the last index being the file.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!