0

The following function...

<script src="//github.com/fengari-lua/fengari-web/releases/download/v0.1.4/fengari-web.js"></script>
<script type="application/lua">

local function SplitPath(path)
    -- Returns the Path, Filename, and Extension as 3 values
    return string.match(path, "^(.-)([^\\/]-)%.([^\\/%.]-)%.?$")
end

local p, f, e = SplitPath("C:\\Users\\Public\\Documents\\rl_my_tool.lua")

print("- Path:      " .. p)
print("- Filename:  " .. f)
print("- Extension: " .. e)

</script>

...that I've got from here seems to do exactly what I need, BUT turns out it returns the extension without the dot (e.g. just "lua" insteaf of ".lua") and I'd want to include it, but I'm so bad when it comes to string patters that even being so close and counting with an explanation in the page (plus this good doc: Understanding Lua Patterns) it seems I simply can't make it work as I'd want... So, could someone please let me a hand on this? Even if it's only a clue... If I don't include any try it's, sincerely, due to I'm practically making random changes to it without (of course ) any success.

EDIT: Just FTR, the same code snippet but working as wanted thanks to Egor's input bellow:

<script src="//github.com/fengari-lua/fengari-web/releases/download/v0.1.4/fengari-web.js"></script>
<script type="application/lua">

local function SplitPath(path)
    -- Returns the Path, Filename, and Extension as 3 values
    return string.match(path, "^(.-)([^\\/]-)(%.[^\\/%.]-)%.?$")
end

local p, f, e = SplitPath("C:\\Users\\Public\\Documents\\rl_my_tool.lua")

print("- Path:      " .. p)
print("- Filename:  " .. f)
print("- Extension: " .. e)

</script>
Rai
  • 314
  • 1
  • 2
  • 9
  • 1
    Just replace `%.([^\\/%.]-)` with `(%.[^\\/%.]-)` – Egor Skriptunoff Oct 23 '22 at 03:48
  • Witchcraft! I'd swear I'd tried everything... Now seriously, I spend the whole night trying to learn something about patterns in Lua and I think I kind of got how _captures_ work (more or less), but your input definitely settles it down and now I think I can clearly see why the **%.** had to be inside of the parenthesis in order to get the dot "captured" with the match and therefore returned, it seems so logic now... Well, thank you! I think I'll add another Code Snipped for the record. – Rai Oct 23 '22 at 12:08

0 Answers0