user2779657
user2779657

Reputation: 99

Extracting filename only with pattern matching

Is it possible to use string.match and return a capture until the last . character to stop at the extension part?

Upvotes: 3

Views: 4921

Answers (1)

Yu Hao
Yu Hao

Reputation: 122383

local str = "filename.lua.txt"
local cap = str:match("(.+)%..+")
print(cap)

Output: filename.lua

The key in this pattern is the greediness of + and to use %. to represent the literal .

Upvotes: 5

Related Questions