3

So I have a string in Lua and I want to remove all occurances ".html" text of the end of the string

local lol = ".com/url/path/stuff.html"

print(lol)

Output i want :

.com/url/path/stuff


local lol2 = ".com/url/path/stuff.html.html"

print(lol2)

Output i want :

.com/url/path/stuff
1
  • bar = lol:gsub("(.*).html.*$","%1") removes one occurance of the .html text but as the "lol2" variable string example i provided it does not work on that :( Commented Aug 16, 2018 at 1:18

2 Answers 2

4

First, you could define a function like this:

function removeHtmlExtensions(s)
    return s:gsub("%.html", "")
end

Then:

local lol = ".com/url/path/stuff.html"
local lol2 = ".com/url/path/stuff.html.html"

local path1 = removeHtmlExtensions(lol)
local path2 = removeHtmlExtensions(lol2)

print(path1) -- .com/url/path/stuff
print(path2) -- .com/url/path/stuff

There is a second value returned from the gsub method that indicates how many times the pattern was matched. It returns, for example, 1 with path1 and 2 with path2. (Just in case that info is useful to you):

local path2, occurrences = removeHtmlExtensions(lol2)

print(occurrences) -- 2
Sign up to request clarification or add additional context in comments.

4 Comments

Is this also ok i got this to work too. bar = lol:gsub(".html.*$","") print(bar) the only issue is if the url contains /html/ it removes that too more problems :(
Since . is a special character in string patterns, you should escape it when you pass to :gsub as :gsub("%.html", "")
@Curtis, right, of course. Thanks for the fix. Updated.
that will remove all occurrences of '.html' though
2

This can easily be done with a tail-recursive function like this:

local function foo(bar)
  if bar:find('%.html$') then return foo(bar:sub(1, -5) else return bar end
end

in words:

  • If bar ends in '.html', remove the last 5 characters and feed that into foo again (to remove any more occurrences)
  • Otherwise, return bar as it is

Benefits:

  • Tail recursive, so it can't overflow the stack even for very long chains '.html'
  • string.find is pretty fast when you anchor the search to the end of the string with $
  • string.sub is also rather fast compared to, for example, string.gsub (note that those two do completely different things, despite the similar name).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.