在Lua中将路径名称拆分成其组件的最巧妙的方法是什么 [英] What is the neatest way to split out a Path Name into its components in Lua
问题描述
我有一个带路径的标准Windows文件名.我需要从字符串中拆分出文件名,扩展名和路径.
I have a standard Windows Filename with Path. I need to split out the filename, extension and path from the string.
我目前只是从末尾开始向后读取字符串,以寻找.切断扩展名,第一个\以获取路径.
I am currently simply reading the string backwards from the end looking for . to cut off the extension, and the first \ to get the path.
我确信我应该可以使用Lua模式来做到这一点,但是在从字符串的右边进行工作时,我一直失败.
I am sure I should be able to do this using a Lua pattern, but I keep failing when it comes to working from the right of the string.
例如 c:\ temp \ test \ myfile.txt 应该返回
eg. c:\temp\test\myfile.txt should return
- c:\ temp \ test \
- myfile.txt
- txt
如果这是重复的话,谢谢您的道歉,但是我可以找到很多其他语言的示例,但不是Lua的示例.
Thank you in advance apologies if this is a duplicate, but I could find lots of examples for other languages, but not for Lua.
推荐答案
> return string.match([[c:\temp\test\myfile.txt]], "(.-)([^\\]-([^%.]+))$")
c:\temp\test\ myfile.txt txt
这似乎完全符合您的要求.
This seems to do exactly what you want.
这篇关于在Lua中将路径名称拆分成其组件的最巧妙的方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!