Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Removing comments from a script?

How would you go about removing comments from this

local scr = [[
local t = "hello world" -- cool
local f = "world hello" -- removing comments
]]
print(scr) --outcome (supposed to be) local t = "hello world"local f = "worldhello"
instead of //--\\ local t = "hello world" -- cool
local f = "world hello" -- removing comments

I tried using string.gsub but also failed, also couldn't find any articles to help me

people are saying this is a solution, but I'm trying to make this

comment_symbols = "--"

s1 = [[
"cool" -- lol
"man"	
]]

print ( string.match( s1, "[^"..comment_symbols.."]+" ) ) -- outputs cool

im trying to make this output "cool man" instead of just "cool"

4 comments

0

Are you talking about the comments highlighted in green or variables?

0

green

0

I think ctrl + f can find and replace stuff in a script. So just put in -- or [[/]] and replace it with nothing

0

I think .gsub is too weak for this, regex would work perfectly fine but regex itself is bigger than Lua itself. Either you can implement 4000 lines of regex yourself, or ignore them.

3 answers

Hello everyone, I know this reply is a year late but this is what I came up with:

local astring  = [[
local a = {
    "foo", -- fa fa fa fa fa
    "bar"
}
]]

print(astring:gsub("--(-.-)\n","\n"))

This prints out:

    local a = {
        "foo", 
        "bar"
    }

1 comment

0

I don't care if you're a year late

I'm a year late, yay.

We need it to ignore string formats because it will mess up the code, right?

This is the multi-lined version

local function GetMutliLinedComments(code)
	local commentfound = false -- if the comments found then it will turn true
	local start_ = 0 -- the start of the comment
	local end_ = 0 -- the end of the comment
	local sub = 0 -- its use is to get the next letter(or word)
	local ignore = false -- if the function can ignore the comment inside a format
	local comments = {}
	code:gsub(".",function(c) -- usin gsub( well it's kinda bad cuz it has a limit
		sub = sub + 1 -- what
		if c == '"'  then -- if it is a string format then
			if ignore then 
				ignore = false
			else
				ignore = true
				return -- not gonna do anything
			end
		end
		
		if c == "'"  then-- if it is a string format then
			if ignore then
				ignore = false
			else
				ignore = true
				return
			end
		end
		if code:sub(sub,sub+1) == "[[" and code:sub(sub-2,sub+1) ~= "--[["  then-- if it is a string format then
			if ignore then
				ignore = false
			else
				print(code:sub(sub-2,sub+1))
				ignore = true
				return
			end
		end
		if not ignore then -- here comes the fun part
			if code:sub(sub,sub+3) == "--[[" and commentfound == false then -- finds a comment format and no comments are found then
				commentfound = true -- a comment is found!
				start_ = sub
			end
			if code:sub(sub,sub+1) == "]]" and commentfound == true then -- finds teh end of a comment format and a comment format is found then
				commentfound = false -- if it's the end of the format no comments are found
				end_ = sub+1 -- just a little bug
				table.insert(comments,code:sub(start_,end_)) -- inserts the comment format into a table
			end
		end
	end)
	return (comments) -- returns the table
end

This discussion is preserved as it was. New answers, comments, and votes are closed.