[ACCEPTED]-What is the neatest way to split out a Path Name into its components in Lua-lua-patterns

Accepted answer
Score: 38

Here is an improved version that works for 3 Windows and Unix paths and also handles 2 files without dots (or files with multiple 1 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"
Score: 18
> return string.match([[c:\temp\test\myfile.txt]], "(.-)([^\\]-([^%.]+))$")
c:\temp\test\   myfile.txt  txt

This seems to do exactly what you want.


Score: 0

Split string in Lua?

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

More Related questions