在 Lua 中将路径名拆分为其组件的最简洁方法是什么
我有一个带路径的标准 Windows 文件名。我需要从字符串中分离出文件名、扩展名和路径。
我目前只是从末尾向后读取字符串以查找 .切断扩展名,第一个 \ 来获取路径。
我确信我应该能够使用 Lua 模式来做到这一点,但是当涉及到从字符串右侧工作时,我总是失败。
例如。 c:\temp\test\myfile.txt 应该返回
- c:\temp\test\
- myfile.txt
- txt
如果这是重复的,请提前致歉,但我可以找到很多其他语言的示例,但不能找到 Lua 的示例。
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.
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.
eg.
c:\temp\test\myfile.txt
should return
- c:\temp\test\
- myfile.txt
- txt
Thank you in advance apologies if this is a duplicate, but I could find lots of examples for other languages, but not for Lua.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个改进版本,适用于 Windows 和 Unix 路径,还可以处理没有点的文件(或有多个点的文件):
Here is an improved version that works for Windows and Unix paths and also handles files without dots (or files with multiple dots):
这似乎正是你想要的。
This seems to do exactly what you want.
在Lua中分割字符串?
那里有一些字符串到表函数,将“\”分割为\ 无论如何都不能出现在文件夹名称中,因此您最终会得到一个表,其中索引一是驱动器,最后一个索引是文件。
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.